微信小程序——頁面滑動事件
wxml:
<view id="id" class = "ball" bindtap = "handletap" bindtouchstart = "handletouchtart" bindtouchmove="handletouchmove" bindtouchend="handletouchend" style = "width : 100%px; height : 40px;"> {{text}} </view>wxss:
.ball { box-shadow:2px 2px 10px #AAA; border-radius: 20px; position: absolute; }js:
//jsPage({ data: { lastX: 0, //滑動開始x軸位置 lastY: 0, //滑動開始y軸位置 text: "沒有滑動", currentGesture: 0, //標識手勢 }, //滑動移動事件 handletouchmove: function (event) { var currentX = event.touches[0].pageX var currentY = event.touches[0].pageY var tx = currentX - this.data.lastX var ty = currentY - this.data.lastY var text = "" //左右方向滑動 if (Math.abs(tx) > Math.abs(ty)) { if (tx < 0) text = "向左滑動" else if (tx > 0) text = "向右滑動" } //上下方向滑動 else { if (ty < 0) text = "向上滑動" else if (ty > 0) text = "向下滑動" } //將當前坐標進行保存以進行下一次計算 this.data.lastX = currentX this.data.lastY = currentY this.setData({ text: text, }); }, //滑動開始事件 handletouchtart: function (event) { this.data.lastX = event.touches[0].pageX this.data.lastY = event.touches[0].pageY }, //滑動結束事件 handletouchend: function (event) { this.data.currentGesture = 0; this.setData({ text: "沒有滑動", }); },})如有疑問請留言或者到本站社區交流討論,感謝閱讀,希望能幫助到大家,謝謝大家對本站的支持!
新聞熱點
疑難解答