小程序swiper轮播CSS3动画及跳转到指定swiper-item实现思路
需要解决的问题近几日一直在看怎样制作微信小程序的swiper轮播图。因为我既需要生成小程序的代码,也需要生成H5版代码,如果编写两套效率会比较低下,所以选择了?uni-app?。
我主要需要解决的是以下几个问题: animate.css
swiper-item
以下就是我整个制作的思路过程,仅供参考。另外,代码是? 代码实现在H5开发中经常使用的就是?animate.css?。在微信中自然是支持的,因为微信会对上传的小程序有大小限制,所以这里我使用了一个极简化的? 我们先来看下代码: <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">
<"swiper-item">
<image src="../../static/uni.png" :class="animate_0"></image>
</view>
</swiper-item>
<"slide1">
<"animate_1"></"slide2">
<"animate_2"></"slide3">
<"animate_3"></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'] = 'animate_2'] = 'animate_3'] = '';
break;
1:
'animate_0'] = '';
2:
3:
'animate_2'] = break;
}
},changeFinish(event){ // swiper动画完成之后,给当前swiper添加动画效果
let current = event.detail.current;
switch(current){
0:
'animate_0'] = 'animated swing';
1:
'animate_1'] = 'animated shake';
'animated tada';
'animated heartBeat';
break;
}
},goChange(){
'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>
我将代码托管到了?腾讯云开发者平台?,需要的话可以参考。在代码目录? 微信小程序代码<!--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' 'animated {{animate_0}}'></image>
</swiper-item>
<swiper-item>
<image src='animated {{animate_1}}'></image>
</swiper-item>
<swiper-item>
<image src='animated {{animate_2}}'></image>
</swiper-item>
</swiper>
</view>
//index.js
const app = getApp()
Page({
data: {
currentId: 0,animate_0: 'swing',animate_1: ''
},onLoad: function() {
},goChange: function() {
this.setData({
currentId: 2
});
},changeSwiper: function(event) {
let current = event.detail.current;
switch (current) {
0:
this.setData({
animate_1: ''
});
break;
1:
this.setData({
animate_0: 2:
break;
}
},changeFinish: function('shake',51); font-weight: 700;">this.setData({
animate_2: 'tada',51); font-weight: 700;">break;
}
}
}) (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |