Home>Article>Web Front-end> 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.
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:
animate.css in swiper
Animation.swiper-item
middle. That is, jump to the specified screen.The following is my entire production process, for reference only. In addition, the code is developed byuni-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 theuni-app
code will be provided for reference.
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 simplifiedanimate.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:
uni-app
supports sass. The concise versionanimate.css
is directly introduced into css.Problem ①circular
This parameter can achieve something similar The H5 page uses the function of swiper.jsloop
parameters. Here I fell intouni-app
and WeChat applet
document description In the pit. Because I have been looking for the parameterloop
(loop), I even thought that this infinite loop function could not be realized. It turns out that this parameter in the applet
is calledcircular
(circular). o(╯□╰)oQuestion③vertical
is set totrue
.uni-app
, viachange
event, you can monitor every change of the carousel screen. In this event, I recorded the subscript of the current screencurrent
. Then cancel all css3 animations onnon-current screens. Finally, in theanimationfinish
event, when theswiper
sliding animation ends, give Add css3 animation to the elements of the current screen.Question②uni-app
The#current-item-id
parameter represents theitem-id
of the current slider. It took me a long time to read this document before I understood it. It turns out thatswiper-item
needs to be specified initem-id
. Then when the user clicks the event, just modify the value bound to current-item-id
. When my code is initialized,item-id
is specified asslide2
on the screen.Question ④uni-app
column. Just setpages.json
#titleNViewto
falseis enough.
WeChat Mini Program Code//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; } } })
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!