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项目如何引入bootstrap、elementUI、echarts
Nov 26 Vue.js
vue 数据操作相关总结
Dec 17 Vue.js
jenkins自动构建发布vue项目的方法步骤
Jan 04 Vue.js
antdesign-vue结合sortablejs实现两个table相互拖拽排序功能
Jan 08 Vue.js
Vue实现下拉加载更多
May 09 Vue.js
vite+vue3.0+ts+element-plus快速搭建项目的实现
Jun 24 Vue.js
Vue3中的Refs和Ref详情
Nov 11 Vue.js
Vue h函数的使用详解
Feb 18 Vue.js
vue3使用vuedraggable实现拖拽功能
Apr 06 Vue.js
vue中控制mock在开发环境使用,在生产环境禁用方式
Apr 06 Vue.js
Vue.Draggable实现交换位置
Apr 07 Vue.js
vue组件vue-esign实现电子签名
Apr 21 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
php流量统计功能的实现代码
2012/09/29 PHP
浅谈PHP中单引号和双引号到底有啥区别呢?
2015/03/04 PHP
CI框架数据库查询之join用法分析
2016/05/18 PHP
jQuery1.6 使用方法二
2011/11/23 Javascript
Jquery带搜索框的下拉菜单
2013/05/06 Javascript
jquery插件开发之实现md5插件
2014/03/17 Javascript
jquery获取checkbox的值并post提交
2015/01/14 Javascript
jQuery插件boxScroll实现图片轮播特效
2015/07/14 Javascript
jQuery实现拖拽页面元素并将其保存到cookie的方法
2016/06/12 Javascript
微信小程序 前端源码逻辑和工作流详解
2016/10/08 Javascript
谈谈JS中的!!
2017/12/07 Javascript
vue登录路由验证的实现
2017/12/13 Javascript
javascript异步编程的六种方式总结
2019/05/17 Javascript
JS document文档的简单操作完整示例
2020/01/13 Javascript
Angular进行简单单元测试的实现方法实例
2020/08/16 Javascript
python如何使用正则表达式的前向、后向搜索及前向搜索否定模式详解
2017/11/08 Python
利用nohup来开启python文件的方法
2019/01/14 Python
python global关键字的用法详解
2019/09/05 Python
使用Python代码实现Linux中的ls遍历目录命令的实例代码
2019/09/07 Python
python进行参数传递的方法
2020/05/12 Python
Python爬取你好李焕英豆瓣短评生成词云的示例代码
2021/02/24 Python
详解python的xlwings库读写excel操作总结
2021/02/26 Python
HTML5 canvas实现的静态循环滚动播放弹幕
2021/01/05 HTML / CSS
荷兰皇家航空公司官方网站:KLM Royal Dutch Airlines
2017/12/07 全球购物
英国最大的纸工艺品商店:CraftStash
2018/12/01 全球购物
Internet体系结构
2014/12/21 面试题
中学教师实习自我鉴定
2013/09/28 职场文书
新闻专业推荐信范文
2013/11/20 职场文书
教师师德演讲稿
2014/05/06 职场文书
管理工程专业求职信
2014/08/10 职场文书
纪念九一八事变演讲稿:牢记九一八,屈辱怎能忘
2014/09/14 职场文书
导游词格式
2015/02/13 职场文书
入党个人总结范文
2015/03/02 职场文书
人事聘任通知
2015/04/21 职场文书
开场白怎么写
2015/06/01 职场文书
python装饰器代码解析
2022/03/23 Python