小程序自定义轮播图

摘要:
话不多说,上图上代码。this.data.done){console.log;this.data.done=true;this.scrollRight();}},//触摸结束事件touchend:function{clearInterval;this.data.time=0;this.data.done=false;},scrollLeft(){varanimation1=wx.createAnimationvaranimation2=wx.createAnimationvaranimation3=wx.createAnimationvaranimation4=wx.createAnimationvaranimatio


话不多说,上图上代码。

小程序自定义轮播图第1张

wxml

<view bindtouchstart="touchstart" bindtouchmove="touchmove" bindtouchend="touchend">
<view animation="{{animation1}}" bindtap="scrollLeft">
<image src="http://img02.tooopen.com/images/20150928/tooopen_sy_143912755726.jpg"/>
<text>{{clubs[0].name}}</text>
</view>
<view animation="{{animation2}}" bindtap="scrollLeft">
<image src="http://img02.tooopen.com/images/20150928/tooopen_sy_143912755726.jpg"/>
<text>{{clubs[1].name}}</text>
</view>
<view animation="{{animation3}}">
<image src="http://img02.tooopen.com/images/20150928/tooopen_sy_143912755726.jpg"/>
<text>{{clubs[2].name}}</text>
</view>
<view animation="{{animation4}}" bindtap="scrollRight">
<image src="http://img02.tooopen.com/images/20150928/tooopen_sy_143912755726.jpg"/>
<text>{{clubs[3].name}}</text>
</view>
<view animation="{{animation5}}" bindtap="scrollRight">
<image src="http://img02.tooopen.com/images/20150928/tooopen_sy_143912755726.jpg"/>
<text>{{clubs[4].name}}</text>
</view>
</view>
wxss
.box {
height: 340rpx;
z-index: 0;
margin: 50rpx 0;
}
.box .club {
height: 140rpx;
140rpx;
position: relative;
display: inline-block;
}
.club image {
height: 140rpx;
140rpx;
}
.club text {
display: block;
100%;
font-size: 24rpx;
line-height: 40rpx;
text-align: center;
}
.box .club:nth-child(1) {
transform: scale(0.8, 0.8) translateX(-120rpx);
opacity: 0.2;
z-index: 10;
}
.box .club:nth-child(2) {
transform: scale(1,1) translateX(-80rpx);
opacity: 0.5;
z-index: 100;
}
.box .club:nth-child(3) {
transform: scale(1.4,1.4);
z-index: 1000;
}
.box .club:nth-child(4) {
transform: scale(1,1) translateX(80rpx);
opacity: 0.5;
z-index: 100;
}
.box .club:nth-child(5) {
transform: scale(0.8, 0.8) translateX(120rpx);
opacity: 0.2;
z-index: 10;
}
.box .club:nth-child(1) text,
.box .club:nth-child(2) text,
.box .club:nth-child(4) text,
.box .club:nth-child(5) text{
visibility: hidden;
}
js
//触摸开始事件
touchstart: function (e) {
console.log(e.touches[0].pageX);
this.data.touchDot = e.touches[0].pageX;
var that = this;
this.data.interval = setInterval(function () {
that.data.time += 1;
}, 100);
},
//触摸移动事件
touchmove: function (e) {
let touchMove = e.touches[0].pageX;
let touchDot = this.data.touchDot;
let time = this.data.time;
console.log("touchMove: " + touchMove + ", touchDot: " + touchDot + ", diff: " + (touchMove - touchDot));
//向左滑动
if (touchMove - touchDot <= -40 && time < 10 && !this.data.done) {
console.log("向左滑动");
this.data.done = true;
this.scrollLeft();
}
//向右滑动
if (touchMove - touchDot >= 40 && time < 10 && !this.data.done) {
console.log("向右滑动");
this.data.done = true;
this.scrollRight();
}
},
//触摸结束事件
touchend: function (e) {
clearInterval(this.data.interval);
this.data.time = 0;
this.data.done = false;
}, scrollLeft() {
var animation1 = wx.createAnimation({
duration: 300,
timingFunction: "linear",
delay: 0
})
var animation2 = wx.createAnimation({
duration: 300,
timingFunction: "linear",
delay: 0
})
var animation3 = wx.createAnimation({
duration: 300,
timingFunction: "linear",
delay: 0
})
var animation4 = wx.createAnimation({
duration: 300,
timingFunction: "linear",
delay: 0
})
var animation5 = wx.createAnimation({
duration: 300,
timingFunction: "linear",
delay: 0
})

this.animation1 = animation1;
this.animation2 = animation2;
this.animation3 = animation3;
this.animation4 = animation4;
this.animation5 = animation5;

this.animation1.translateX(-60).opacity(0).step();
this.animation2.translateX(-140).opacity(0.5).scale(0.8, 0.8).step();
this.animation3.translateX(-110).opacity(0.5).scale(1, 1).step();
this.animation4.translateX(-70).opacity(1).scale(1.4, 1.4).step();
this.animation5.translateX(-30).opacity(0.5).scale(1, 1).step();

this.setData({
animation1: animation1.export(),
animation2: animation2.export(),
animation3: animation3.export(),
animation4: animation4.export(),
animation5: animation5.export()
})

var that = this;
setTimeout(function () {
that.animation1.translateX(-50).opacity(0.2).scale(0.8, 0.8).step({ duration: 0, timingFunction: 'linear' });
that.animation2.translateX(-40).opacity(0.5).scale(1, 1).step({ duration: 0, timingFunction: 'linear' });
that.animation3.translateX(0).opacity(1).scale(1.4, 1.4).step({ duration: 0, timingFunction: 'linear' });
that.animation4.translateX(40).opacity(0.5).scale(1, 1).step({ duration: 0, timingFunction: 'linear' });
that.animation5.translateX(50).opacity(0.2).scale(0.8, 0.8).step({ duration: 0, timingFunction: 'linear' });
that.setData({
animation1: animation1.export(),
animation2: animation2.export(),
animation3: animation3.export(),
animation4: animation4.export(),
animation5: animation5.export()
})
}.bind(this), 195)

let array = this.data.clubs;
let shift = array.shift();
array.push(shift);

setTimeout(function () {
this.setData({
clubs: array
})
}.bind(this), 195)
},

//向右滑动事件
scrollRight() {
var animation1 = wx.createAnimation({
duration: 300,
timingFunction: "linear",
delay: 0
})
var animation2 = wx.createAnimation({
duration: 300,
timingFunction: "linear",
delay: 0
})
var animation3 = wx.createAnimation({
duration: 300,
timingFunction: "linear",
delay: 0
})
var animation4 = wx.createAnimation({
duration: 300,
timingFunction: "linear",
delay: 0
})
var animation5 = wx.createAnimation({
duration: 300,
timingFunction: "linear",
delay: 0
})

this.animation1 = animation1;
this.animation2 = animation2;
this.animation3 = animation3;
this.animation4 = animation4;
this.animation5 = animation5;

this.animation1.translateX(30).opacity(0.5).scale(1, 1).step();
this.animation2.translateX(70).opacity(1).scale(1.4, 1.4).step();
this.animation3.translateX(110).opacity(0.5).scale(1, 1).step();
this.animation4.translateX(120).opacity(0.2).scale(0.8, 0.8).step();
this.animation5.translateX(130).opacity(0).step();

this.setData({
animation1: animation1.export(),
animation2: animation2.export(),
animation3: animation3.export(),
animation4: animation4.export(),
animation5: animation5.export()
})

var that = this;
setTimeout(function () {
that.animation1.translateX(-50).opacity(0.2).scale(0.8, 0.8).step({ duration: 0, timingFunction: 'linear' });
that.animation2.translateX(-40).opacity(0.5).scale(1, 1).step({ duration: 0, timingFunction: 'linear' });
that.animation3.translateX(0).opacity(1).scale(1.4, 1.4).step({ duration: 0, timingFunction: 'linear' });
that.animation4.translateX(40).opacity(0.5).scale(1, 1).step({ duration: 0, timingFunction: 'linear' });
that.animation5.translateX(50).opacity(0.2).scale(0.8, 0.8).step({ duration: 0, timingFunction: 'linear' });
that.setData({
animation1: animation1.export(),
animation2: animation2.export(),
animation3: animation3.export(),
animation4: animation4.export(),
animation5: animation5.export()
})
}.bind(this), 195)

let array = this.data.clubs;
let pop = array.pop();
array.unshift(pop);

setTimeout(function () {
this.setData({
clubs: array
})
}.bind(this), 195)
}
复制完我还有几句话要说,做前端千万不要被框架限制住自己,要做一个有灵魂的工程师。就像这个轮播图千万不要被小程序的swiper所限制。

免责声明:文章转载自《小程序自定义轮播图》仅用于学习参考。如对内容有疑问,请及时联系本站处理。

上篇APP 半自适应 WEB页面在 IntelliJ IDEA 中调出类似于 Eclipse 中的“方法大纲”视图的方法下篇

宿迁高防,2C2G15M,22元/月;香港BGP,2C5G5M,25元/月 雨云优惠码:MjYwNzM=

相关文章

picker-view、微信小程序自定义时间选择器(非官方)

picker-view自定义时间选择器 官网的自定义时间选择器比较简陋、日期不准 下面是我自己写的一个demo <view class="baseList"> <view class="list clearfix"> <view class="fl listName"><text class="req...

android RecyclerView

引用 在项目的build.gradle添加依赖 compile 'com.android.support:recyclerview-v7:23.4.0' RecyclierView使用的基本方法 recyclerView.setAdapter(); 添加适配器(必须) recyclerView.setLayoutManager(); 选择一种布局(必须)...

Android 自定义View及其在布局文件中的使用示例

前言:     尽管Android已经为我们提供了一套丰富的控件,如:Button,ImageView,TextView,EditText等众多控件,但是,有时候在项目开发过程中,还是需要开发者自定义一些需要重复使用的控件,使之能像Android提供的其它控件一样,使用起来方便,幸好Android为我们自定义控件过程扫除了障碍,提供了一套基础的类(如:Vi...

Django框架rest_framework中APIView的as_view()源码解析、认证、权限、频率控制

在前后端分离项目中前面我们也提到了各种认证需要自己来做,那么我们用rest_framework的时候 rest_framework也为我们提供相应的接口,rest_framework中的APIView实现了和Django原生View  as_view()一样的功能 并在此基础上实现了原生request的封装、认证、权限、视图等等功能 我们来看APIView...

swiper 点击切换,拖动切换后继续自动轮播

HTML结构 <div className="swiper-container main_meeting"> <div className="swiper-wrapper"> <div className="swiper-slide">Slide 1</div> <div class...

轮播图3D效果--roundabout(兼容IE8)升级版

<!DOCTYPE html> <html> <head lang="en"> <meta charset="UTF-8"> <title></title> <style> *{ margin:0;...