微信小程序监听左右滑动事件,进行切换操作
页面:
<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 () {
}
})
posted on 2020-02-03 22:20 object360 阅读(3928) 评论(0) 编辑 收藏 举报
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· 阿里巴巴 QwQ-32B真的超越了 DeepSeek R-1吗?
· 【译】Visual Studio 中新的强大生产力特性
· 【设计模式】告别冗长if-else语句:使用策略模式优化代码结构
· 10年+ .NET Coder 心语 ── 封装的思维:从隐藏、稳定开始理解其本质意义