先看案例,使用vue+swiper实现,slide不同高度时,动态计算盒子高度,让其下方高度自适应的效果
首先搭建vue项目,这里不做过多说明,然后安装swiper
npm install swiper --save-dev
1. js部分:初始化swiper组件,vue要在mounted生命周期中进行初始化,代码如下:
import Swiper from 'swiper' import { TweenMax, Power2 } from 'gsap'
初始化时调用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使用方法
// 动态计算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="/UploadFiles/2021-04-02/wish.png">注意: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; }这样就实现了一个高度自适应的轮播效果了,三个及以上也没问题啦,喜欢点个关注吧,嘻嘻~
免责声明:本站文章均来自网站采集或用户投稿,网站不提供任何软件下载或自行开发的软件!
如有用户或公司发现本站内容信息存在侵权行为,请邮件告知! 858582#qq.com
白云城资源网 Copyright www.dyhadc.com
暂无“vue仿携程轮播图效果(滑动轮播,下方高度自适应)”评论...
更新日志
2024年05月20日
2024年05月20日
- 张琍敏1978-雪中莲[台湾复刻版][WAV+CUE]
- 叶蕴仪1993-睡美人[日本版][WAV+CUE]
- 夜晚助兴音乐-群星《新时代床头音乐-性能量》2CD[WAV]
- 24K德国HD金碟《历届奥斯卡获奖金曲》3CD[WAV整轨]
- 邰正宵《重燃爱恋 贰 Walk On》[FLAC/分轨][431.72MB]
- 苏文劭《雨停出来走走》[320K/MP3][81.11MB]
- 苏文劭《雨停出来走走》[FLAC/分轨][210.76MB]
- 群星《2024好听新歌04》十倍音质WAV分轨
- 陈宁《弹指之间HQ》头版限量[低速原抓WAV+CUE]
- 陈宁《故人还》HQCDII限量签名版[低速原抓WAV+CUE]
- 苏文劭《春曲(Lessons)》[320K/MP3][39.8MB]
- 苏文劭《春曲(Lessons)》[FLAC/分轨][97.78MB]
- 群星《2006香港高级视听展原音精选 SACD》[ISO][2G]
- 张琍敏1977-枫林小雨[台湾复刻版][WAV+CUE]
- 林一峰2014-COOKINGMUSIC[香港首版][WAV+CUE]