微信小程序監聽左右滑動事件,進行切換操作


頁面:
<van-tabs class="tabs-main" active="{{ tab.active }}" bind:change="onChange" bindtouchstart='touchstart' bindtouchmove='touchmove' bindtouchend='touchend'>
 
</van-tabs>
 
JS:
 
// pages/subjectOne/subjectOne.js

//觸屏開始點坐標
var startDot = {
X: 0,
Y: 0
};
//觸屏到點坐標
var touchDot = {
X: 0,
Y: 0
};
var time = 0; //觸屏時間
var number; //定時器ID




Page({

/**
* 頁面的初始數據
*/
data: {
 
},


onAnswerRaidonChange(event) {
this.setData({
radio: event.detail
});


console.log(this.data);
},




/**
* 觸屏開始計時和獲取坐標
*/
touchstart: function (event) {
startDot.X = event.touches[0].pageX;
startDot.Y = event.touches[0].pageY;
number = setInterval(function () { time++; }, 100);
},
/**
* 判斷滑動距離和時間是否需要切換頁面
*/
touchmove: function (event) {
touchDot.X = event.touches[0].pageX;
touchDot.Y = event.touches[0].pageY;
//向左滑處理
if ((startDot.X - touchDot.X) > 200 && (startDot.Y - touchDot.Y) < 150 && time < 5 && time > 0.1) {
wx.showToast({
title: '左',
icon: 'success',
duration: 2000
})

clearInterval(number);
time = 0;
}
//向右滑處理
if ((touchDot.X - startDot.X) > 200 && (touchDot.Y - startDot.Y) < 150 && time < 5 && time > 0.1) {
wx.showToast({
title: '右',
icon: 'success',
duration: 2000
})
clearInterval(number);
time = 0;
}
},
/**
* 結束觸屏重置計時
*/
touchend: function (event) {
clearInterval(number);
time = 0;
},


/**
* 生命周期函數--監聽頁面加載
*/
onLoad: function (options) {

},

/**
* 生命周期函數--監聽頁面初次渲染完成
*/
onReady: function () {

},

/**
* 生命周期函數--監聽頁面顯示
*/
onShow: function () {

},

/**
* 生命周期函數--監聽頁面隱藏
*/
onHide: function () {

},

/**
* 生命周期函數--監聽頁面卸載
*/
onUnload: function () {

},

/**
* 頁面相關事件處理函數--監聽用戶下拉動作
*/
onPullDownRefresh: function () {

},

/**
* 頁面上拉觸底事件的處理函數
*/
onReachBottom: function () {

},

/**
* 用戶點擊右上角分享
*/
onShareAppMessage: function () {

}
})


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM