我爱模板网 > 建站教程 > JS、jQ、TS >  微信小程序 监听手势滑动切换页面实例详解_Ja正文

微信小程序 监听手势滑动切换页面实例详解_Ja

微信小程序 监听手势滑动切换页面实例详解

1.对应的xml里写上手势开始、滑动、结束的监听:

1<view class="touch" bindtouchstart="touchStart" bindtouchmove="touchMove" bindtouchend="touchEnd" ></view>

2.js:

1var touchDot = 0;//触摸时的原点
2var time = 0;// 时间记录,用于滑动时且时间小于1s则执行左右滑动
3var interval = "";// 记录/清理时间记录
4Page({
5 data: {...}
6   })
01Page({
02 data: {
03     ...
04 },
05 // 触摸开始事件
06 touchStart: function (e) {
07  touchDot = e.touches[0].pageX; // 获取触摸时的原点
08  // 使用js计时器记录时间 
09  interval = setInterval(function () {
10   time++;
11  }, 100);
12 },
13 // 触摸移动事件
14 touchMove: function (e) {
15  var touchMove = e.touches[0].pageX;
16  console.log("touchMove:" + touchMove + " touchDot:" + touchDot + " diff:" + (touchMove - touchDot));
17  // 向左滑动 
18  if (touchMove - touchDot <= -40 && time < 10) {
19   wx.switchTab({
20    url: '../左滑页面/左滑页面'
21   }); 
22  }
23  // 向右滑动
24  if (touchMove - touchDot >= 40 && time < 10) {
25   console.log('向右滑动');
26   wx.switchTab({
27    url: '../右滑页面/右滑页面'
28   }); 
29  }
30 },
31 // 触摸结束事件
32 touchEnd: function (e) {
33  clearInterval(interval); // 清除setInterval
34  time = 0;
35 },
36.
37.
38.
39})

感谢阅读,希望能帮助到大家,谢谢大家对本站的支持!



部分素材资源来源网站,本站提供免费下载,如有侵权请联系站长马上删除!
上一篇:微信小程序canvas写字板效果及实例_JavaScript 下一篇:详解原生js实现offset方法_javascript技巧
发表评论
请自觉遵守互联网相关的政策法规,严禁发布色情、暴力、反动的言论。
选择头像:
最新评论

猜你喜欢