微信小程序——左右滑动切换页面事件
微信小程序的左右滑动触屏事件,主要有三个事件:touchstart,touchmove,touchend。
这三个事件最重要的属性是pageX和pageY,表示X,Y坐标。
touchstart在触摸开始时触发事件;
touchend在触摸结束时触发事件;
touchmove触摸的过程中不断激发这个事件;
这三个事件都有一个timeStamp的属性,查看timeStamp属性,可以看到顺序是touchstart => touchmove=> touchmove => ··· =>touchmove =>touchend。
第一步:在wxml文件中绑定事件(需要左右滑动的界面)
1 2 3 | <view class="container" bindtouchstart="touchStart" bindtouchmove="touchMove" bindtouchend="touchEnd">
// do something
</view>
|
第二步:在js文件中处理左右滑动逻辑
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 | var touchDot = 0;
var time = 0;
var interval = "";
var nth = 0;
var nthMax = 5;
var tmpFlag = true;
touchStart:function(e){
touchDot = e.touches[0].pageX;
interval = setInterval(function(){
time++;
},100);
},
touchMove:function(e){
var touchMove = e.touches[0].pageX;
console.log("touchMove:"+touchMove+" touchDot:"+touchDot+" diff:"+(touchMove - touchDot));
if(touchMove - touchDot <= -40 && time < 10){
if(tmpFlag && nth < nthMax){
var tmp = this.data.menu.map(function (arr, index) {
tmpFlag = false;
if(arr.active){
nth = index;
++nth;
arr.active = nth > nthMax ? true : false;
}
if(nth == index){
arr.active = true;
name = arr.value;
}
return arr;
})
this.getNews(name);
this.setData({menu : tmp});
}
}
if(touchMove - touchDot >= 40 && time < 10){
if(tmpFlag && nth > 0){
nth = --nth < 0 ? 0 : nth;
var tmp = this.data.menu.map(function (arr, index) {
tmpFlag = false;
arr.active = false;
if(nth == index){
arr.active = true;
name = arr.value;
}
return arr;
})
this.getNews(name);
this.setData({menu : tmp});
}
}
},
touchEnd:function(e){
clearInterval(interval);
time = 0;
tmpFlag = true;
},
|
以上就是全部内容。
版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。