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用mixin合并重复代码的实现
Nov 27 Vue.js
vue3.0+vue-router+element-plus初实践
Dec 02 Vue.js
Vue通过阿里云oss的url连接直接下载文件并修改文件名的方法
Dec 25 Vue.js
在vue项目中封装echarts的步骤
Dec 25 Vue.js
如何在VUE中使用vue-awesome-swiper
Jan 04 Vue.js
vue实现按钮切换图片
Jan 20 Vue.js
使用vue3重构拼图游戏的实现示例
Jan 25 Vue.js
vue穿梭框实现上下移动
Jan 29 Vue.js
Vue如何实现组件间通信
May 15 Vue.js
Vue鼠标滚轮滚动切换路由效果的实现方法
Aug 04 Vue.js
vue自定义右键菜单之全局实现
Apr 09 Vue.js
vue实力踩坑之push当前页无效
Apr 10 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
一个简单计数器的源代码
2006/10/09 PHP
php发送http请求的常用方法分析
2016/11/08 PHP
无语,javascript居然支持中文(unicode)编程!
2007/04/12 Javascript
jQuery数组处理代码详解(含实例演示)
2012/02/03 Javascript
JavaScript 代码压缩工具小结
2012/02/27 Javascript
JavaScript基础篇之变量作用域、传值、传址的简单介绍与实例
2013/06/29 Javascript
jquery的相对父元素和相对文档定位示例代码
2013/08/02 Javascript
Flexigrid在IE下不显示数据的处理的解决方法
2013/10/24 Javascript
js数组方法扩展实现数组统计函数
2014/04/09 Javascript
使用DNode实现php和nodejs之间通信的简单实例
2015/07/06 NodeJs
js实现n秒倒计时后才可以点击的效果
2015/12/20 Javascript
vue-cli扩展多模块打包的示例代码
2018/04/09 Javascript
jQuery分组选择器简单用法示例
2019/04/04 jQuery
微信小程序实现音频文件播放进度的实例代码
2020/03/02 Javascript
node.js基础知识汇总
2020/08/25 Javascript
[36:13]Mineski vs iG 2018国际邀请赛小组赛BO2 第一场 8.16
2018/08/17 DOTA
零基础写python爬虫之使用urllib2组件抓取网页内容
2014/11/04 Python
Python多线程编程(五):死锁的形成
2015/04/05 Python
对于Python的框架中一些会话程序的管理
2015/04/20 Python
使用rpclib进行Python网络编程时的注释问题
2015/05/06 Python
numpy.random模块用法总结
2019/05/27 Python
python爬虫刷访问量 2019 7月
2019/08/01 Python
python通过SSH登陆linux并操作的实现
2019/10/10 Python
python 和c++实现旋转矩阵到欧拉角的变换方式
2019/12/04 Python
使用Keras预训练模型ResNet50进行图像分类方式
2020/05/23 Python
python3.7 openpyxl 在excel单元格中写入数据实例
2020/09/01 Python
html5 postMessage前端跨域并前端监听的方法示例
2018/11/01 HTML / CSS
平面设计岗位职责
2013/12/14 职场文书
宣传策划类求职信范文
2014/01/31 职场文书
经典商业广告词
2014/03/13 职场文书
2014最新毕业证代领委托书
2014/09/26 职场文书
公司离职证明标准格式
2014/11/18 职场文书
证婚人致辞精选
2015/07/28 职场文书
卫生主题班会
2015/08/14 职场文书
2015年社区反邪教工作总结
2015/10/14 职场文书
Java使用Unsafe类的示例详解
2021/09/25 Java/Android