vue仿携程轮播图效果(滑动轮播,下方高度自适应)


Posted in Vue.js onFebruary 11, 2021

先看案例,使用vue+swiper实现,slide不同高度时,动态计算盒子高度,让其下方高度自适应的效果

vue仿携程轮播图效果(滑动轮播,下方高度自适应)

首先搭建vue项目,这里不做过多说明,然后安装swiper

npm install swiper --save-dev

1. js部分:初始化swiper组件,vue要在mounted生命周期中进行初始化,代码如下:

import Swiper from 'swiper'
import { TweenMax, Power2 } from 'gsap'

vue仿携程轮播图效果(滑动轮播,下方高度自适应)

初始化时调用resize函数,计算屏幕容器的宽高,代码如下

// 重新计算屏幕宽高
resize(swiper) {
	this.clientWidth = document.documentElement.clientWidth||document.body.clientWidth;
	this.clientHeight = document.documentElement.clientHeight||document.body.clientHeight;
	this.draw(swiper)
},

计算完后调用draw函数,根据滑动slide,动态计算轮播容器的高度;注意这里引用了TweenMax框架,使用前需要安装,详细使用方法可参考官网TweenMax

npm install gsap -D

先大概看下TweenMax使用方法

vue仿携程轮播图效果(滑动轮播,下方高度自适应)

// 动态计算swiper-container高度
			draw(swiper) {
				TweenMax.to(this.tweenObj, 0.5, {translate: swiper.translate, ease: Power2.easeOut,
					onUpdate: () => {
						let translate = this.tweenObj.translate
						// 左边slide索引
						let iLeft = Math.floor(-translate / this.clientWidth)
						if (iLeft > this.slidesLength) {
							iLeft = this.slidesLength
						}
						// 右边slide索引
						let iRight = iLeft + 1
						if (iRight > this.slidesLength) {
							iRight = this.slidesLength
						}
						for(let i=0; i< this.swiperSlide.length; i++){
							//图片宽度满屏时,每个图片的高度
							this.swiperSlide[i].fullHeight = this.clientWidth/this.swiperSlide[i].getBoundingClientRect().width * this.swiperSlide[i].getBoundingClientRect().height;
						}
						//移动比例 移动过程中高度变化 0~1~0的变化规律
						let percent = Number((-translate / this.clientWidth).toFixed(5)) - iLeft
						//根据左右图片和移动比例得出相应高度
						let currentHeight = (this.swiperSlide[iRight].fullHeight - this.swiperSlide[iLeft].fullHeight )*percent + this.swiperSlide[iLeft].fullHeight
						// 轮播容器高度
						swiper.el.style.height = currentHeight +'px'
					}
				})
			}

2.html部分

<!--仿携程轮播效果-->
	<div class="swiper-demo">
		<div class="swiper-container">
			<div class="swiper-wrapper">
			<!--这里一定要加高度,不然会出问题!!!-->
				<div class="swiper-slide" style="height: 222px;">
					<div class="wrap" v-for="(item, index) in category1" :key="index">
						<img src="../assets/wish.png" alt="">
						<span>{{item.name}}</span>
					</div>
				</div>
					<!--这里一定要加高度,不然会出问题!!!-->
				<div class="swiper-slide" style="height: 400px;">
					<div class="wrap" v-for="(item, index) in category2" :key="index">
						<img src="../assets/wish.png" alt="">
						<span>{{item.name}}</span>
					</div>
				</div>
			</div>
		</div>

		<div style="background: salmon; height: 80vh">随便写自己的UI</div>
	</div>

注意:swiper-slide一定要加高度,不然会出问题

3.css部分

.swiper-slide {
		width: auto;
		height: 100%;
		display: flex;
		flex-wrap: wrap;
		justify-content: space-between;
	}
	.wrap {
		width: 24%;
		height: 100px;
		display: flex;
		flex-direction: column;
		align-items: center;
		justify-content: center;
	}
	img {
		width: 60px;
	}

这样就实现了一个高度自适应的轮播效果了,三个及以上也没问题啦,喜欢点个关注吧,嘻嘻~

vue仿携程轮播图效果(滑动轮播,下方高度自适应)

到此这篇关于vue仿携程轮播图效果(滑动轮播,下方高度自适应)的文章就介绍到这了,更多相关vue轮播图内容请搜索三水点靠木以前的文章或继续浏览下面的相关文章希望大家以后多多支持三水点靠木!

Vue.js 相关文章推荐
如何在vue中使用kindeditor富文本编辑器
Dec 19 Vue.js
vue下拉刷新组件的开发及slot的使用详解
Dec 23 Vue.js
vue实现登录、注册、退出、跳转等功能
Dec 23 Vue.js
vue调用微信JSDK 扫一扫,相册等需要注意的事项
Jan 03 Vue.js
基于vue-simple-uploader封装文件分片上传、秒传及断点续传的全局上传插件功能
Feb 23 Vue.js
vue常用高阶函数及综合实例
Feb 25 Vue.js
vue-cropper组件实现图片切割上传
May 27 Vue.js
vue3使用vue-router的完整步骤记录
Jun 20 Vue.js
vue项目如何打包之项目打包优化(让打包的js文件变小)
Apr 30 Vue.js
vue postcss-px2rem 自适应布局
May 15 Vue.js
vue递归实现树形组件
Jul 15 Vue.js
Vue深入理解插槽slot的使用
Aug 05 Vue.js
Vue+Bootstrap实现简易学生管理系统
Feb 09 #Vue.js
详解Vue的七种传值方式
Feb 08 #Vue.js
Vue中使用wangeditor富文本编辑的问题
Feb 07 #Vue.js
vue使用lodop打印控件实现浏览器兼容打印的方法
Feb 07 #Vue.js
vue如何使用rem适配
Feb 06 #Vue.js
如何管理Vue中的缓存页面
Feb 06 #Vue.js
手动实现vue2.0的双向数据绑定原理详解
Feb 06 #Vue.js
You might like
CodeIgniter php mvc框架 中国网站
2008/05/26 PHP
Ajax+PHP 边学边练 之二 实例
2009/11/24 PHP
php调用mysql数据 dbclass类
2011/05/07 PHP
浅析PHP echo 和 print 语句
2020/06/30 PHP
javascript当onmousedown、onmouseup、onclick同时应用于同一个标签节点Element
2010/01/05 Javascript
EXTJS内使用ACTIVEX控件引起崩溃问题的解决方法
2010/03/31 Javascript
jquery实现像栅栏一样左右滑出式二级菜单效果代码
2015/08/24 Javascript
详解JavaScript 中的 replace 方法
2016/01/01 Javascript
js实现Tab选项卡切换效果
2020/07/17 Javascript
vue如何进行动画的封装
2018/09/26 Javascript
微信小程序当前时间时段选择器插件使用方法详解
2018/12/28 Javascript
bootstrap 日期控件 datepicker被弹出框dialog覆盖的解决办法
2019/07/09 Javascript
Vue如何获取数据列表展示
2019/12/11 Javascript
微信小程序获取当前时间及星期几的实例代码
2020/09/20 Javascript
[01:37]TI4西雅图DOTA2前线报道 VG拿下首胜教练357给出获胜秘诀
2014/07/10 DOTA
[03:09]2014DOTA2国际邀请赛 赛场上的美丽风景线 中国Coser也爱DOTA2
2014/07/20 DOTA
Python计算开方、立方、圆周率,精确到小数点后任意位的方法
2018/07/17 Python
Python解析json时提示“string indices must be integers”问题解决方法
2019/07/31 Python
Python从文件中读取指定的行以及在文件指定位置写入
2019/09/06 Python
python super的使用方法及实例详解
2019/09/25 Python
Python参数传递及收集机制原理解析
2020/06/05 Python
法国最大电子商务平台:Cdiscount
2018/03/13 全球购物
意大利奢侈品购物网站:Deliberti
2019/10/08 全球购物
韩国商务邀请函
2014/01/14 职场文书
大二法英学生职业生涯规划范文
2014/02/27 职场文书
讲座主持词
2014/03/20 职场文书
乔布斯斯坦福大学演讲稿
2014/05/23 职场文书
祖国在我心中演讲稿(小学生)
2014/09/23 职场文书
2015年推普周活动总结
2015/03/27 职场文书
个人维稳承诺书
2015/05/04 职场文书
毕业论文答辩开场白和答辩技巧
2015/05/27 职场文书
我在伊朗长大观后感
2015/06/16 职场文书
行政处罚告知书
2015/07/01 职场文书
“5.12”护士节主持词
2015/07/04 职场文书
大学优秀学生主要事迹材料
2015/11/04 职场文书
Python使用socket去实现TCP客户端和TCP服务端
2022/04/12 Python