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
When customizing the installation of software, yo...
This article shares the installation and configur...
What is a carousel? Carousel: In a module or wind...
Effect (source code at the end): accomplish: 1. D...
In the trend of gradual transition from tradition...
1. Usage scenarios There is such a requirement, s...
To use Nginx under Windows, we need to master som...
The current environment is: Centos 7.5 docker-ce ...
Related system calls for file operations create i...
Install Docker Desktop Download address: Docker D...
Inside the style tag of the vue component, there ...
Preface: Due to my work, I am involved in the fie...
What you will learn 1. Software installation and ...
This article example shares the specific code of ...
Since I need to learn how to build servers and da...