本人微信公衆號: 前端修煉之路,歡迎關注。
近幾日一直在看怎樣製做微信小程序的swiper輪播圖。由於我既須要生成小程序的代碼,也須要生成H5版代碼,若是編寫兩套效率會比較低下,因此選擇了uni-app。css
uni-app
已經在基礎組件swiper中已經直接支持了輪播動畫。前端
我主要須要解決的是如下幾個問題:css3
animate.css
動畫。swiper-item
中。也就是跳轉到指定的屏。如下就是我整個製做的思路過程,僅供參考。另外,代碼是uni-app
開發,因此在小程序中和H5中測試都沒有問題。另外爲了方便小程序
開發同窗瞭解,會提供小程序
版代碼和uni-app
代碼供參考。git
在H5開發中常常使用的就是animate.css。在微信中天然是支持的,由於微信會對上傳的小程序有大小限制,因此這裏我使用了一個極簡化的animate.css
,其中刪掉了不少-webkit-animation
開頭的css3。由於咱們只須要在小程序和H5中運行,這樣作影響也不大。若是須要的話,能夠從下面的代碼中獲取。github
咱們先來看下代碼:web
<template> <view class="content"> <button type="primary" @tap="goChange">跳轉到第二屏</button> <swiper class="content-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 class="swiper-item"> <image src="../../static/uni.png" :class="animate_0"></image> </view> </swiper-item> <swiper-item item-id="slide1"> <view class="swiper-item"> <image src="../../static/uni.png" :class="animate_1"></image> </view> </swiper-item> <swiper-item item-id="slide2"> <view class="swiper-item"> <image src="../../static/uni.png" :class="animate_2"></image> </view> </swiper-item> <swiper-item item-id="slide3"> <view class="swiper-item"> <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){ // 清空除了當前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 lang="scss"> @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中直接引入了簡潔版animate.css
。問題① circular
這個參數能夠實現相似H5頁面使用swiper.jsloop
參數的功能。這裏我掉到了uni-app
和微信小程序
文檔描述的坑中。由於一直在找loop
(循環)這個參數,我甚至都覺得實現不了這個無限循環的功能了呢。原來小程序
中這個參數叫作circular
(圓形)。o(╯□╰)o 問題③ vertical
設置爲true
。uni-app
中,經過change
事件,能夠監聽每個輪播屏的改變。在這個事件中,我記錄的當前屏的下標current
。而後將非當前屏的所有css3動畫取消掉。最後在animationfinish
事件中,當swiper
滑動動畫結束後,給當前屏的元素添加css3動畫。問題② uni-app
中有個current-item-id
參數,表明當前所在滑塊的 item-id
。這個文檔我看了很久,才明白。原來是須要在swiper-item
中指定上item-id
。而後當用戶點擊事件觸發時,修改綁定到current-item-id
上的值便可。個人代碼初始化時指定到了item-id
爲slide2
這一屏上。問題④ uni-app
中隱藏掉H5導航欄。只須要在pages.json
中設置titleNView
爲false
便可。<!--index.wxml--> <view class="container"> <button bindtap='goChange'>跳轉到</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; } } })
我將代碼託管到了騰訊雲開發者平臺,須要的話能夠參考。在代碼目錄unpackage/dist/build/h5
中,就是生成好的H5版頁面。須要注意的是,要部署到web服務器使用,不支持本地file協議打開。
其中生成了兩個版本的代碼,方便你們參考。json