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 elementUI 使用el-select 时 change事件的触发问题
Nov 17 Vue.js
解决vue页面刷新,数据丢失的问题
Nov 24 Vue.js
解决vue下载后台传过来的乱码流的问题
Dec 05 Vue.js
浅谈Vue使用Elementui修改默认的最快方法
Dec 05 Vue.js
vue+openlayers绘制省市边界线
Dec 24 Vue.js
vue 实现基础组件的自动化全局注册
Dec 25 Vue.js
Vue2.x-使用防抖以及节流的示例
Mar 02 Vue.js
详解vue中v-for的key唯一性
May 15 Vue.js
HTML+VUE分页实现炫酷物联网大屏功能
May 27 Vue.js
SSM VUE Axios详解
Oct 05 Vue.js
详解Vue中$props、$attrs和$listeners的使用方法
Feb 18 Vue.js
vue中this.$http.post()跨域和请求参数丢失的解决
Apr 08 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中的字符串函数
2006/11/24 PHP
php+ajax做仿百度搜索下拉自动提示框(有实例)
2012/08/21 PHP
php绘图之生成饼状图的方法
2015/01/24 PHP
Yii框架中jquery表单验证插件用法示例
2016/10/18 PHP
PHP信号处理机制的操作代码讲解
2019/04/19 PHP
innerHTML,outerHTML,innerTEXT三者之间的区别
2007/01/28 Javascript
父子窗体间传递JSON格式的数据的代码
2010/12/25 Javascript
jquery实现textarea 高度自适应
2015/03/11 Javascript
使用Chrome浏览器调试AngularJS应用的方法
2015/06/18 Javascript
解决angular的$http.post()提交数据时后台接收不到参数值问题的方法
2015/12/10 Javascript
js简单判断移动端系统的方法
2016/02/25 Javascript
javascript判断图片是否加载完成的方法推荐
2016/05/13 Javascript
[02:25]DOTA2英雄基础教程 熊战士
2014/01/03 DOTA
Python下的Softmax回归函数的实现方法(推荐)
2017/01/26 Python
Python连接SQLServer2000的方法详解
2017/04/19 Python
Python中几种导入模块的方式总结
2017/04/27 Python
使用Python实现 学生学籍管理系统
2019/11/26 Python
python实现磁盘日志清理的示例
2020/11/05 Python
Python WebSocket长连接心跳与短连接的示例
2020/11/24 Python
Python爬虫分析微博热搜关键词的实现代码
2021/02/22 Python
CSS3 box-shadow属性实例详解
2020/06/19 HTML / CSS
Perricone MD裴礼康美国官网:抗衰老护肤品
2016/09/26 全球购物
Myprotein芬兰官网:欧洲第一运动营养品牌
2019/05/05 全球购物
Tenstickers法国:墙贴和装饰贴纸
2019/08/26 全球购物
Myprotein中国网站:欧洲畅销运动营养品牌
2021/02/11 全球购物
abstract class和interface有什么区别?
2012/01/03 面试题
商务日语专业毕业生求职信
2013/10/26 职场文书
日语专业毕业生自荐信
2013/11/11 职场文书
大学系主任推荐信范文
2013/12/24 职场文书
护理专业自我鉴定
2014/01/30 职场文书
《伯牙绝弦》教学反思
2014/03/02 职场文书
大学生党员学习焦裕禄精神思想汇报
2014/09/10 职场文书
2015年团支部工作总结
2015/04/03 职场文书
2015年班组建设工作总结
2015/05/13 职场文书
环境保护宣传标语大全!
2019/06/28 职场文书
Python数据处理的三个实用技巧分享
2022/04/01 Python