本人微信公众号: 前端修炼之路,欢迎关注。
近几日一直在看怎样制做微信小程序的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