小程序判断滑动方向

偏执的太偏执、 2022-05-11 07:40 314阅读 0赞
  1. WXML:
  2. <view id="id" bindtouchstart="handletouchtart" bindtouchmove="handletouchmove"
  3. style="width:100%px;height:80px;line-height:80px;color:#fff;text-align:center; background:red">
  4. {
  5. {text}}
  6. </view>
  7. index.js
  8. Page({
  9. data: {
  10. lastX: 0,
  11. lastY: 0,
  12. text: "没有滑动",
  13. },
  14. handletouchmove: function (event) {
  15. // console.log(event)
  16. let currentX = event.touches[0].pageX
  17. let currentY = event.touches[0].pageY
  18. let tx = currentX - this.data.lastX
  19. let ty = currentY - this.data.lastY
  20. let text = ""
  21. if (Math.abs(tx) > Math.abs(ty)) {
  22. //左右方向滑动
  23. if (tx < 0)
  24. text = "向左滑动"
  25. else if (tx > 0)
  26. text = "向右滑动"
  27. }
  28. else {
  29. //上下方向滑动
  30. if (ty < 0)
  31. text = "向上滑动"
  32. else if (ty > 0)
  33. text = "向下滑动"
  34. }
  35. //将当前坐标进行保存以进行下一次计算
  36. this.data.lastX = currentX
  37. this.data.lastY = currentY
  38. this.setData({
  39. text: text,
  40. });
  41. },
  42. handletouchtart: function (event) {
  43. // console.log(event)
  44. // 赋值
  45. this.data.lastX = event.touches[0].pageX
  46. this.data.lastY = event.touches[0].pageY
  47. }
  48. })

转载地址:https://www.cnblogs.com/changxue/p/8466836.html

发表评论

表情:
评论列表 (有 0 条评论,314人围观)

还没有评论,来说两句吧...

相关阅读