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 相关文章推荐
深入了解Vue3模板编译原理
Nov 19 Vue.js
在vue中动态修改css其中一个属性值操作
Dec 07 Vue.js
Vue-router中hash模式与history模式的区别详解
Dec 15 Vue.js
vue 动态生成拓扑图的示例
Jan 03 Vue.js
如何在VUE中使用vue-awesome-swiper
Jan 04 Vue.js
Vue 集成 PDF.js 实现 PDF 预览和添加水印的步骤
Jan 22 Vue.js
vue form表单post请求结合Servlet实现文件上传功能
Jan 22 Vue.js
vue如何使用rem适配
Feb 06 Vue.js
Vue自定义铃声提示音组件的实现
Jan 22 Vue.js
vue使用wavesurfer.js解决音频可视化播放问题
Apr 04 Vue.js
Vue OpenLayer测距功能的实现
Apr 20 Vue.js
vue使用watch监听属性变化
Apr 30 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正则提取或替换img标记属性
2013/06/26 PHP
php判断文件夹是否存在不存在则创建
2015/04/09 PHP
php中文乱码问题的终极解决方案汇总
2017/08/01 PHP
PHP利用DWZ.CN服务生成短网址
2019/08/11 PHP
jquery自动完成插件(autocomplete)应用之PHP版
2009/12/15 Javascript
js判断选择的时间是否大于今天的代码
2013/08/20 Javascript
JavaScript子类用Object.getPrototypeOf去调用父类方法解析
2013/12/05 Javascript
javascript解三阶幻方(九宫格)
2015/04/22 Javascript
javascript数组克隆简单实现方法
2015/12/16 Javascript
jquery插件autocomplete用法示例
2016/07/01 Javascript
详解JavaScript RegExp对象
2017/02/04 Javascript
angular2+nodejs实现图片上传功能
2017/03/27 NodeJs
仿京东快报向上滚动的实例
2017/12/13 Javascript
浅谈react-router HashRouter和BrowserRouter的使用
2017/12/29 Javascript
浅谈vue的props,data,computed变化对组件更新的影响
2018/01/16 Javascript
jQuery实现导航样式布局操作示例【可自定义样式布局】
2018/07/24 jQuery
使用Angular-CLI构建NPM包的方法
2018/09/07 Javascript
vue-cli项目中使用echarts图表实例
2018/10/22 Javascript
vue-router权限控制(简单方式)
2018/10/29 Javascript
js序列化和反序列化的使用讲解
2019/01/19 Javascript
vue cli4下环境变量和模式示例详解
2020/04/09 Javascript
简单的Python抓taobao图片爬虫
2014/10/26 Python
Python函数和模块的使用总结
2019/05/20 Python
python实现感知机线性分类模型示例代码
2019/06/02 Python
python scrapy爬虫代码及填坑
2019/08/12 Python
8段用于数据清洗Python代码(小结)
2019/10/31 Python
Python开发入门——迭代的基本使用
2020/09/03 Python
Desigual德国官网:在线购买原创服装
2018/03/27 全球购物
工商管理专业应届生求职信
2013/11/04 职场文书
廉洁自律承诺书
2014/03/27 职场文书
学校关爱留守儿童活动方案
2014/08/27 职场文书
街道务虚会发言材料
2014/10/20 职场文书
react合成事件与原生事件的相关理解
2021/05/13 Javascript
正确使用MySQL INSERT INTO语句
2021/05/26 MySQL
浅谈Go语言多态的实现与interface使用
2021/06/16 Golang
Python学习之时间包使用教程详解
2022/03/21 Python