swiper实现轮播效果
這期內(nèi)容當(dāng)中小編將會(huì)給大家?guī)碛嘘P(guān)swiper實(shí)現(xiàn)輪播效果,以專業(yè)的角度為大家分析和敘述,閱讀完這篇文章希望大家可以有所收獲。
需要解決的問題
近幾日一直在看怎樣制作微信小程序的swiper輪播圖。因?yàn)槲壹刃枰尚〕绦虻拇a,也需要生成H5版代碼,如果編寫兩套效率會(huì)比較低下,所以選擇了uni-app。
uni-app已經(jīng)在基礎(chǔ)組件swiper中已經(jīng)直接支持了輪播動(dòng)畫。
我主要需要解決的是以下幾個(gè)問題:
- ①在swiper中怎樣添加css3流行的
animate.css動(dòng)畫。 - ②添加好后如果滑動(dòng)了輪播圖,怎樣能保證下一屏的動(dòng)畫不自動(dòng)播放。
- ③怎樣能實(shí)現(xiàn)輪播圖的無限循環(huán)播放。
- ④怎樣能實(shí)現(xiàn),當(dāng)用戶點(diǎn)擊一個(gè)按鈕之后,可以跳轉(zhuǎn)到指定的
swiper-item中。也就是跳轉(zhuǎn)到指定的屏。 - ⑤小程序和H5版的代碼會(huì)生成一個(gè)頭部,在H5版中需要隱藏掉導(dǎo)航欄。
以下就是我整個(gè)制作的思路過程,僅供參考。另外,代碼是uni-app開發(fā),所以在小程序中和H5中測(cè)試都沒有問題。另外為了方便小程序開發(fā)同學(xué)了解,會(huì)提供小程序版代碼和uni-app代碼供參考。
代碼實(shí)現(xiàn)
在H5開發(fā)中經(jīng)常使用的就是animate.css。在微信中自然是支持的,因?yàn)槲⑿艜?huì)對(duì)上傳的小程序有大小限制,所以這里我使用了一個(gè)極簡(jiǎn)化的animate.css,其中刪掉了很多-webkit-animation開頭的css3。因?yàn)槲覀冎恍枰谛〕绦蚝虷5中運(yùn)行,這樣做影響也不大。如果需要的話,可以從下面的代碼中獲取。
我們先來看下代碼:
<template> <view> <button type="primary" @tap="goChange">跳轉(zhuǎn)到第二屏</button> <swiper :vertical="true" :indicator-dots="true" :autoplay="false" :interval="3000" :duration="1000" @change="changeSwiper" @animationfinish="changeFinish" :current-item-id="item_id" circular="true"> <swiper-item item-id="slide0"> <view> <image src="../../static/uni.png" :class="animate_0"></image> </view> </swiper-item> <swiper-item item-id="slide1"> <view> <image src="../../static/uni.png" :class="animate_1"></image> </view> </swiper-item> <swiper-item item-id="slide2"> <view> <image src="../../static/uni.png" :class="animate_2"></image> </view> </swiper-item> <swiper-item item-id="slide3"> <view> <image src="../../static/uni.png" :class="animate_3"></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){ // 清空除了當(dāng)前swiper以外的所有動(dòng)畫 let current = event.detail.current; // 當(dāng)前頁下標(biāo) this.item_id = 'slide'+current; // 這里必須記錄,否則只能跳轉(zhuǎn)一次 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動(dòng)畫完成之后,給當(dāng)前swiper添加動(dòng)畫效果 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>
- 首先
uni-app支持sass。在css中直接引入了簡(jiǎn)潔版animate.css。問題① - 之后通過查看文檔,發(fā)現(xiàn)
circular這個(gè)參數(shù)可以實(shí)現(xiàn)類似H5頁面使用swiper.jsloop參數(shù)的功能。這里我掉到了uni-app和微信小程序文檔描述的坑中。因?yàn)橐恢痹谡?code>loop(循環(huán))這個(gè)參數(shù),我甚至都以為實(shí)現(xiàn)不了這個(gè)無限循環(huán)的功能了呢。原來小程序中這個(gè)參數(shù)叫做circular(圓形)。o(╯□╰)o問題③ - 因?yàn)槲疫@里要實(shí)現(xiàn)一個(gè)豎屏的滑動(dòng)效果,所以將參數(shù)
vertical設(shè)置為true。 - 在
uni-app中,通過change事件,可以監(jiān)聽每一個(gè)輪播屏的改變。在這個(gè)事件中,我記錄的當(dāng)前屏的下標(biāo)current。然后將非當(dāng)前屏的全部css3動(dòng)畫取消掉。最后在animationfinish事件中,當(dāng)swiper滑動(dòng)動(dòng)畫結(jié)束后,給當(dāng)前屏的元素添加css3動(dòng)畫。問題② - 在
uni-app中有個(gè)current-item-id參數(shù),代表當(dāng)前所在滑塊的item-id。這個(gè)文檔我看了好久,才明白。原來是需要在swiper-item中指定上item-id。然后當(dāng)用戶點(diǎn)擊事件觸發(fā)時(shí),修改綁定到current-item-id上的值即可。我的代碼初始化時(shí)指定到了item-id為slide2這一屏上。問題④ - 最后一個(gè)問題時(shí)
uni-app中隱藏掉H5導(dǎo)航欄。只需要在pages.json中設(shè)置titleNView為false即可。
微信小程序代碼
<!--index.wxml--> <view> <button bindtap='goChange'>跳轉(zhuǎn)到</button> <swiper vertical="true" circular="true" current="{{currentId}}" indicator-dots="true" bindchange="changeSwiper" bindanimationfinish="changeFinish"> <swiper-item> <image src='../../static/uni.png' class='animated {{animate_0}}'></image> </swiper-item> <swiper-item> <image src='../../static/uni.png' class='animated {{animate_1}}'></image> </swiper-item> <swiper-item> <image src='../../static/uni.png' class='animated {{animate_2}}'></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; } } })
我將代碼托管到了騰訊云開發(fā)者平臺(tái),需要的話可以參考。在代碼目錄unpackage/dist/build/h6中,就是生成好的H5版頁面。需要注意的是,要部署到web服務(wù)器使用,不支持本地file協(xié)議打開。
其中生成了兩個(gè)版本的代碼,方便大家參考。
總結(jié)
以上是生活随笔為你收集整理的swiper实现轮播效果的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: window10系统 同时安装支持 jd
- 下一篇: 电脑360浏览器如何保存网页账号密码