


Mini program swiper carousel CSS3 animation and use to jump to specified swiper-item
My WeChat public account: Front-end Cultivation Road, welcome to follow me.
Problems that need to be solved
In recent days, I have been looking at how to create a swiper carousel for WeChat mini programs. Because I need to generate both the code for the mini program and the H5 version of the code, it would be inefficient to write two sets, so I chose uni-app.
uni-app
Already directly supports carousel animation in the basic component swiper.
What I mainly need to solve are the following problems:
-
①How to add the popular css3
<span style="font-size: 14px;">animate.css in swiper </span>
Animation. - ②If you slide the carousel image after adding it, how can you ensure that the animation on the next screen does not play automatically.
- ③How to achieve infinite loop playback of carousel images.
-
④How can it be achieved? When the user clicks a button, he can jump to the specified
<span style="font-size: 14px;">swiper-item</span>
middle. That is, jump to the specified screen. - ⑤The code of the applet and H5 version will generate a header, and the navigation bar needs to be hidden in the H5 version.
The following is my entire production process, for reference only. In addition, the code is developed by uni-app
, so there is no problem in testing in mini programs and H5. In addition, in order to facilitate the understanding of 小program
developers, the 小program
version code and the uni-app
code will be provided for reference.
Code implementation
animate.css is often used in H5 development. It is naturally supported in WeChat, because WeChat has size restrictions on uploaded mini programs, so here I used a very simplified animate.css
, which deleted a lot of -webkit- css3 starting with animation
. Because we only need to run in small programs and H5, this will not have much impact. You can get it from the code below if needed.
Let’s take a look at the code first:
<template> <view> <button>跳转到第二屏</button> <swiper> <swiper-item> <view> <image></image> </view> </swiper-item> <swiper-item> <view> <image></image> </view> </swiper-item> <swiper-item> <view> <image></image> </view> </swiper-item> <swiper-item> <view> <image></image> </view> </swiper-item> </swiper> </view> </template> <script> export default { data() { return { item_id: 'slide2', animate_0: 'animated swing', animate_1: '', animate_2: '', animate_3: '' } }, onLoad() { }, methods: { changeSwiper(event){ // 清空除了当前swiper以外的所有动画 let current = event.detail.current; // 当前页下标 this.item_id = 'slide'+current; // 这里必须记录,否则只能跳转一次 switch (current){ case 0: this['animate_1'] = this['animate_2'] = this['animate_3'] = ''; break; case 1: this['animate_0'] = this['animate_2'] = this['animate_3'] = ''; break; case 2: this['animate_0'] = this['animate_1'] = this['animate_3'] = ''; break; case 3: this['animate_0'] = this['animate_1'] = this['animate_2'] = ''; break; } }, changeFinish(event){ // swiper动画完成之后,给当前swiper添加动画效果 let current = event.detail.current; switch(current){ case 0: this['animate_0'] = 'animated swing'; break; case 1: this['animate_1'] = 'animated shake'; break; case 2: this['animate_2'] = 'animated tada'; break; case 3: this['animate_3'] = 'animated heartBeat'; break; } }, goChange(){ this.item_id = 'slide1'; } } } </script> <style> @import '../../common/animate.css'; .content { text-align: center; .content-swiper{ height: 100vh; image{ height: 200upx; width: 200upx; margin-top: 200upx; } } } </style>
-
First of all
<span style="font-size: 14px;">uni-app</span>
supports sass. The concise version<span style="font-size: 14px;">animate.css</span>
is directly introduced into css. Problem ① -
After checking the document, I found that
<span style="font-size: 14px;">circular</span>
This parameter can achieve something similar The H5 page uses the function of swiper.js<span style="font-size: 14px;">loop</span>
parameters. Here I fell into<span style="font-size: 14px;">uni-app</span>
and<span style="font-size: 14px;"> WeChat applet</span>
document description In the pit. Because I have been looking for the parameter<span style="font-size: 14px;">loop</span>
(loop), I even thought that this infinite loop function could not be realized. It turns out that this parameter in the<span style="font-size: 14px;"> applet</span>
is called<span style="font-size: 14px;">circular</span>
(circular). o(╯□╰)o Question③ -
Because I want to achieve a vertical screen sliding effect here, so the parameters
<span style="font-size: 14px;">vertical</span>
is set to<span style="font-size: 14px;">true</span>
. -
In
<span style="font-size: 14px;">uni-app</span>
, via<span style="font-size: 14px;">change</span>
event, you can monitor every change of the carousel screen. In this event, I recorded the subscript of the current screen<span style="font-size: 14px;">current</span>
. Then cancel all css3 animations on non-current screens. Finally, in the<span style="font-size: 14px;">animationfinish</span>
event, when the<span style="font-size: 14px;">swiper</span>
sliding animation ends, give Add css3 animation to the elements of the current screen. Question② -
There is a ## in
<span style="font-size: 14px;"></span>uni-app
The#current-item-id<span style="font-size: 14px;"></span>
parameter represents theitem-id<span style="font-size: 14px;"></span>
of the current slider. It took me a long time to read this document before I understood it. It turns out thatswiper-item<span style="font-size: 14px;"></span>
needs to be specified initem-id<span style="font-size: 14px;"></span>
. Then when the user clicks the event, just modify the value bound to <span style="font-size: 14px;"></span>current-item-id<span style="font-size: 14px;"></span>
. When my code is initialized,item-id<span style="font-size: 14px;"></span>
is specified asslide2<span style="font-size: 14px;"></span>
on the screen. Question ④ - The last question is to hide the H5 navigation in
uni-app<span style="font-size: 14px;"></span>
column. Just setpages.json<span style="font-size: 14px;"></span>
#titleNViewto <span style="font-size: 14px;"></span>
falseis enough. <span style="font-size: 14px;"></span>
WeChat Mini Program Code
<!--index.wxml--> <view> <button>跳转到</button> <swiper> <swiper-item> <image></image> </swiper-item> <swiper-item> <image></image> </swiper-item> <swiper-item> <image></image> </swiper-item> </swiper> </view> //index.js const app = getApp() Page({ data: { currentId: 0, animate_0: 'swing', animate_1: '', animate_2: '' }, onLoad: function() { }, goChange: function() { this.setData({ currentId: 2 }); }, changeSwiper: function(event) { let current = event.detail.current; switch (current) { case 0: this.setData({ animate_1: '', animate_2: '' }); break; case 1: this.setData({ animate_0: '', animate_2: '' }); break; case 2: this.setData({ animate_0: '', animate_1: '' }); break; } }, changeFinish: function(event) { let current = event.detail.current; switch (current) { case 0: this.setData({ animate_0: 'swing', }); break; case 1: this.setData({ animate_1: 'shake', }); break; case 2: this.setData({ animate_2: 'tada', }); break; } } })
I hosted the code on the Tencent Cloud Developer Platform, you can refer to it if necessary. In the code directory unpackage/dist/build/h5, there is the generated H5 version page. It should be noted that if it is deployed to a web server, it does not support opening the local file protocol.
Two versions of the code are generated for your reference. Recommended tutorial: "
WeChat Mini Program
"
The above is the detailed content of Mini program swiper carousel CSS3 animation and use to jump to specified swiper-item. For more information, please follow other related articles on the PHP Chinese website!

JavaScript is widely used in websites, mobile applications, desktop applications and server-side programming. 1) In website development, JavaScript operates DOM together with HTML and CSS to achieve dynamic effects and supports frameworks such as jQuery and React. 2) Through ReactNative and Ionic, JavaScript is used to develop cross-platform mobile applications. 3) The Electron framework enables JavaScript to build desktop applications. 4) Node.js allows JavaScript to run on the server side and supports high concurrent requests.

Python is more suitable for data science and automation, while JavaScript is more suitable for front-end and full-stack development. 1. Python performs well in data science and machine learning, using libraries such as NumPy and Pandas for data processing and modeling. 2. Python is concise and efficient in automation and scripting. 3. JavaScript is indispensable in front-end development and is used to build dynamic web pages and single-page applications. 4. JavaScript plays a role in back-end development through Node.js and supports full-stack development.

C and C play a vital role in the JavaScript engine, mainly used to implement interpreters and JIT compilers. 1) C is used to parse JavaScript source code and generate an abstract syntax tree. 2) C is responsible for generating and executing bytecode. 3) C implements the JIT compiler, optimizes and compiles hot-spot code at runtime, and significantly improves the execution efficiency of JavaScript.

JavaScript's application in the real world includes front-end and back-end development. 1) Display front-end applications by building a TODO list application, involving DOM operations and event processing. 2) Build RESTfulAPI through Node.js and Express to demonstrate back-end applications.

The main uses of JavaScript in web development include client interaction, form verification and asynchronous communication. 1) Dynamic content update and user interaction through DOM operations; 2) Client verification is carried out before the user submits data to improve the user experience; 3) Refreshless communication with the server is achieved through AJAX technology.

Understanding how JavaScript engine works internally is important to developers because it helps write more efficient code and understand performance bottlenecks and optimization strategies. 1) The engine's workflow includes three stages: parsing, compiling and execution; 2) During the execution process, the engine will perform dynamic optimization, such as inline cache and hidden classes; 3) Best practices include avoiding global variables, optimizing loops, using const and lets, and avoiding excessive use of closures.

Python is more suitable for beginners, with a smooth learning curve and concise syntax; JavaScript is suitable for front-end development, with a steep learning curve and flexible syntax. 1. Python syntax is intuitive and suitable for data science and back-end development. 2. JavaScript is flexible and widely used in front-end and server-side programming.

Python and JavaScript have their own advantages and disadvantages in terms of community, libraries and resources. 1) The Python community is friendly and suitable for beginners, but the front-end development resources are not as rich as JavaScript. 2) Python is powerful in data science and machine learning libraries, while JavaScript is better in front-end development libraries and frameworks. 3) Both have rich learning resources, but Python is suitable for starting with official documents, while JavaScript is better with MDNWebDocs. The choice should be based on project needs and personal interests.


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

ZendStudio 13.5.1 Mac
Powerful PHP integrated development environment

EditPlus Chinese cracked version
Small size, syntax highlighting, does not support code prompt function

MinGW - Minimalist GNU for Windows
This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.

SublimeText3 Chinese version
Chinese version, very easy to use

Notepad++7.3.1
Easy-to-use and free code editor