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

4 solutions to CSS browser compatibility issues

Front-end is a tough job, not only because techno...

WeChat applet implementation anchor positioning function example

Preface In the development of small programs, we ...

Solutions to problems using addRoutes in Vue projects

Table of contents Preface 1. 404 Page 1. Causes 2...

CSS3 realizes draggable Rubik's Cube 3D effect

Mainly used knowledge points: •css3 3d transforma...

MySQL Practical Experience of Using Insert Statement

Table of contents 1. Several syntaxes of Insert 1...

Four ways to combine CSS and HTML

(1) Each HTML tag has an attribute style, which c...

How to deploy nodejs service using Dockerfile

Initialize Dockerfile Assuming our project is nam...

Beginner's guide to building a website ⑥: Detailed usage of FlashFXP

Today I will introduce the most basic functions of...

How does Vue download non-same-origin files based on URL

Generally speaking, we can have the following two...

MariaDB-server installation of MySQL series

Table of contents Tutorial Series 1. Install Mari...

Detailed explanation of js's event loop event queue in the browser

Table of contents Preface Understanding a stack a...

Steps to create a WEBSERVER using NODE.JS

Table of contents What is nodejs Install NodeJS H...