Vue imitates Ctrip's carousel effect (sliding carousel, highly adaptive below)

Vue imitates Ctrip's carousel effect (sliding carousel, highly adaptive below)

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.

insert image description here

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' 

insert image description here

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.

insert image description here

// 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>

Note: swiper-slide must increase the height, otherwise there will be problems

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~

insert image description here

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:
  • Vue implements the idea and example analysis of picture carousel component
  • Sample code for implementing image carousel in Vue without operating DOM
  • Vue custom js picture fragment carousel switching effect implementation code
  • Detailed explanation of the idea of ​​using Vue to create a picture carousel component
  • Vue encapsulates Swiper to realize the image carousel effect
  • An example of how to use Vue to implement a mobile image carousel component
  • Using vueJs to implement image carousel example code
  • VUE develops a sample code for a picture carousel component
  • Realizing the image carousel effect based on vue.js
  • Vue implements automatic sliding carousel pictures

<<:  Initial settings after installing Ubuntu 16 in the development environment

>>:  Summary of several important performance index calculation and optimization methods for MySQL

Recommend

Detailed explanation of Linux environment variable configuration strategy

When customizing the installation of software, yo...

JavaScript to achieve a simple carousel effect

What is a carousel? Carousel: In a module or wind...

Design of pop-up windows and floating layers in web design

In the trend of gradual transition from tradition...

js to realize automatic lock screen function

1. Usage scenarios There is such a requirement, s...

How to use Dockerfile to create a mirror of the Java runtime environment

The current environment is: Centos 7.5 docker-ce ...

Detailed explanation of Linux file operation knowledge points

Related system calls for file operations create i...

A very detailed tutorial on installing rocketmq under Docker Desktop

Install Docker Desktop Download address: Docker D...

Perfect solution to the problem of webpack packaging css background image path

Inside the style tag of the vue component, there ...

Detailed explanation of nginx proxy_cache cache configuration

Preface: Due to my work, I am involved in the fie...

Detailed explanation of MySQL from getting started to giving up - installation

What you will learn 1. Software installation and ...

js drag and drop table to realize content calculation

This article example shares the specific code of ...

Mysql classic high-level/command line operation (quick) (recommended)

Since I need to learn how to build servers and da...