Let's look at the case first. Use vue+swiper to implement it. When the slide has different heights, the box height is dynamically calculated to make the height below it adaptive. First, build the vue project. I won’t explain it in detail here. Then install swiper. npm install swiper --save-dev 1. js part: initialize the swiper component. vue needs to be initialized in the mounted life cycle. The code is as follows: import Swiper from 'swiper' import { TweenMax, Power2 } from 'gsap' The resize function is called during initialization to calculate the width and height of the screen container. The code is as follows // Recalculate screen width and height resize(swiper) { this.clientWidth = document.documentElement.clientWidth||document.body.clientWidth; this.clientHeight = document.documentElement.clientHeight||document.body.clientHeight; this.draw(swiper) }, After the calculation, the draw function is called to dynamically calculate the height of the carousel container according to the sliding slide. Note that the TweenMax framework is referenced here and needs to be installed before use. For detailed usage methods, please refer to the official website TweenMax npm install gsap -D Let's take a look at how to use TweenMax first. // Dynamically calculate swiper-container height draw(swiper) { TweenMax.to(this.tweenObj, 0.5, {translate: swiper.translate, ease: Power2.easeOut, onUpdate: () => { let translate = this.tweenObj.translate // Left slide index let iLeft = Math.floor(-translate / this.clientWidth) if (iLeft > this.slidesLength) { iLeft = this.slidesLength } // Right slide index let iRight = iLeft + 1 if (iRight > this.slidesLength) { iRight = this.slidesLength } for(let i=0; i< this.swiperSlide.length; i++){ //When the image width is full screen, the height of each image this.swiperSlide[i].fullHeight = this.clientWidth/this.swiperSlide[i].getBoundingClientRect().width * this.swiperSlide[i].getBoundingClientRect().height; } //Movement ratio The height changes from 0 to 1 to 0 during movement let percent = Number((-translate / this.clientWidth).toFixed(5)) - iLeft //According to the left and right pictures and the movement ratio, the corresponding height is obtained let currentHeight = (this.swiperSlide[iRight].fullHeight - this.swiperSlide[iLeft].fullHeight )*percent + this.swiperSlide[iLeft].fullHeight // Carousel container height swiper.el.style.height = currentHeight + 'px' } }) } 2.html part <!--Imitating Ctrip's carousel effect--> <div class="swiper-demo"> <div class="swiper-container"> <div class="swiper-wrapper"> <!--You must increase the height here, otherwise there will be problems! ! ! --> <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> <!--You must increase the height here, otherwise there will be problems! ! ! --> <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">Write your own UI</div> </div>
3.css part .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; } This way, a highly adaptive carousel effect is achieved. Three or more carousels are no problem. If you like it, please follow us. Hehe~ This is the end of this article about the Vue imitation Ctrip carousel effect (sliding carousel, highly adaptive below). For more relevant Vue carousel content, please search 123WORDPRESS.COM's previous articles or continue to browse the following related articles. I hope everyone will support 123WORDPRESS.COM in the future! You may also be interested in:
|
<<: Initial settings after installing Ubuntu 16 in the development environment
>>: Summary of several important performance index calculation and optimization methods for MySQL
Front-end is a tough job, not only because techno...
Preface In the development of small programs, we ...
Table of contents Preface 1. 404 Page 1. Causes 2...
Mainly used knowledge points: •css3 3d transforma...
Table of contents 1. Several syntaxes of Insert 1...
This article shares the download and installation...
(1) Each HTML tag has an attribute style, which c...
Initialize Dockerfile Assuming our project is nam...
Today I will introduce the most basic functions of...
Generally speaking, we can have the following two...
Table of contents Tutorial Series 1. Install Mari...
Table of contents Preface Understanding a stack a...
Table of contents Problem Overview Problem Reprod...
Preface Every developer who comes into contact wi...
Table of contents What is nodejs Install NodeJS H...