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项目实现主题切换的多种方法
Nov 26 Vue.js
使用vue编写h5公众号跳转小程序的实现代码
Nov 27 Vue.js
vue中defineProperty和Proxy的区别详解
Nov 30 Vue.js
vue实现图书管理系统
Dec 29 Vue.js
基于Vue2实现移动端图片上传、压缩、拖拽排序、拖拽删除功能
Jan 05 Vue.js
Vue实现摇一摇功能(兼容ios13.3以上)
Jan 26 Vue.js
Vue中避免滥用this去读取data中数据
Mar 02 Vue.js
vite2.0+vue3移动端项目实战详解
Mar 03 Vue.js
如何理解Vue前后端数据交互与显示
May 10 Vue.js
vue+springboot实现登录验证码
May 27 Vue.js
Vue CLI中模式与环境变量的深入详解
May 30 Vue.js
Vue自定义铃声提示音组件的实现
Jan 22 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
[EPIC] Larva vs Flash ZvT @ Crossing Field [2017-10-09]
2020/03/17 星际争霸
Thinkphp搜索时首页分页和搜索页保持条件分页的方法
2014/12/05 PHP
WebQQ最新登陆协议的用法
2014/12/22 PHP
php文件上传的两种实现方法
2016/04/04 PHP
IE的有条件注释判定IE版本详解(附实例代码)
2012/01/04 Javascript
js中substring和substr两者区别和使用方法
2015/11/09 Javascript
javascript淘宝主图放大镜功能
2016/10/20 Javascript
js实现PC端根据IP定位当前城市地理位置
2017/02/22 Javascript
原生JavaScript实现Ajax异步请求
2017/11/19 Javascript
浅谈针对Vue相同路由不同参数的刷新问题
2018/09/29 Javascript
js实现倒计时器自定义时间和暂停
2019/02/25 Javascript
JS+CSS3实现的简易钟表效果示例
2019/04/13 Javascript
微信小程序中如何使用flyio封装网络请求
2019/07/03 Javascript
[02:49]DOTA2完美大师赛首日观众采访
2017/11/23 DOTA
Python中使用HTMLParser解析html实例
2015/02/08 Python
简单的Apache+FastCGI+Django配置指南
2015/07/22 Python
Python中动态创建类实例的方法
2017/03/24 Python
python爬虫 正则表达式使用技巧及爬取个人博客的实例讲解
2017/10/20 Python
Python3读取Excel数据存入MySQL的方法
2018/05/04 Python
详解Python with/as使用说明
2018/12/13 Python
对Python Class之间函数的调用关系详解
2019/01/23 Python
python字符串循环左移
2019/03/08 Python
详解python运行三种方式
2019/05/13 Python
django 装饰器 检测登录状态操作
2020/07/02 Python
Python如何实现大型数组运算(使用NumPy)
2020/07/24 Python
芬兰攀岩、山地运动和户外活动用品购物网站:Bergfreunde
2016/10/06 全球购物
J2EE包括哪些技术
2016/11/25 面试题
毕业生的求职信范文分享
2013/12/04 职场文书
中层干部竞争上岗演讲稿
2014/01/13 职场文书
公立医院改革实施方案
2014/03/14 职场文书
房产委托公证书样本
2014/04/04 职场文书
2015年项目经理工作总结
2015/04/30 职场文书
务工证明怎么写
2015/06/18 职场文书
食品卫生管理制度
2015/08/06 职场文书
剖析后OpLog订阅MongoDB的数据变更就没那么难了
2022/02/24 MongoDB
部分武汉产收音机展览
2022/04/07 无线电