当前位置:网站首页>小程序更多的手势事件(左右滑动、放大缩小、双击、长按)
小程序更多的手势事件(左右滑动、放大缩小、双击、长按)
2022-08-01 07:10:00 【大橘为重¨】
前言
- 微信小程序提供的原生事件有:触摸开始(bindtouchstart)、移动触摸点(bindtouchmove)、触摸结束(bindtouchend)以及长按事件(bindlongtap)和单击事件(bindtap)。通过以上原生事件可设计制作衍生出更多的手势事件;
- 文章记录小程序中组件 “左右滑动、放大缩小、双击” 事件的配置;
- “长按” 事件通过原生事件即可配置
一、组件事件的设置
<view class="new-event-test" bindtouchstart="myTouchStart" bindtouchmove="myTouchMove" bindtouchend="myTouchEnd" bindlongtap="myLongTap" bindtap="myTap">
</view>
二、左右滑动事件
- 涉及原生事件有 “bindtouchstart”、“bindtouchmove”
1、流程图
- 左事件流程图:

- 右事件流程图:

2、代码示例
变量记录:
let startPoint = 0, //记录滑动的初始位置
let slipFlag = false, //定义 滑动事件 节流阀, 防止一次滑动触发多次滑动事件
bindtouchstart事件:
myTouchStart(e) {
// ---------------------记录滑动事件信息---------------------
//开启滑动事件
slipFlag = true
//记录触摸点的坐标信息
startPoint = e.touches[0]
//---------------------记录滑动事件信息end---------------------
},
bindtouchmove事件:
myTouchMove(e) {
// ----------------监听手势左右滑事件----------------
if (((startPoint.clientX - e.touches[e.touches.length - 1].clientX) > 80) && slipFlag) {
console.log("左滑事件");
slipFlag = false
return
} else if (((startPoint.clientX - e.touches[e.touches.length - 1].clientX) < -80) && slipFlag) {
console.log("右滑事件");
slipFlag = false
return
}
// ----------------监听手势左右滑事件end----------------
},
三、放大缩小事件
- 涉及原生事件有 “bindtouchstart”、“bindtouchmove”、“bindtouchend”
1、流程图
放大事件流程图:

右事件流程图:

2、代码示例
变量记录:
let zoomFlag = false, //定义 缩放事件 节流阀,防止一次缩放触发两次缩放事件
let distance = 0, //记录手指移动距离
let scale = 1, //定义初始化的页面缩放大小
let newScale = 0, //记录新的页面缩放大小
bindtouchstart事件:
myTouchStart(e) {
//---------------------记录缩放事件信息---------------------
// 当两根手指放上去的时候,将距离(distance)初始化。
let xMove = e.touches[1].clientX - e.touches[0].clientX;
let yMove = e.touches[1].clientY - e.touches[0].clientY;
//计算开始触发两个手指坐标的距离
distance = Math.sqrt(xMove * xMove + yMove * yMove);
//---------------------记录缩放事件信息end---------------------
},
bindtouchmove事件:
myTouchMove(e) {
// ----------------监听手势缩小放大事件----------------
// 单手指缩放不做任何操作
if (e.touches.length != 1) {
//双手指运动 x移动后的坐标和y移动后的坐标
let xMove = e.touches[1].clientX - e.touches[0].clientX;
let yMove = e.touches[1].clientY - e.touches[0].clientY;
//双手指运动新的 ditance
let newDistance = Math.sqrt(xMove * xMove + yMove * yMove);
//计算移动的过程中实际移动了多少的距离
let distanceDiff = newDistance - distance;
newScale = scale + 0.005 * distanceDiff
// 打开缩放监听
zoomFlag = true
return
}
// ----------------监听手势缩小放大事件end----------------
},
bindtouchend事件:
myTouchEnd() {
if (zoomFlag) {
if (newScale > 1.3) {
console.log("放大了");
} else if (newScale < 0.7) {
console.log("缩小了");
}
// 关闭缩放监听
zoomFlag = false
}
},
四、双击事件
- 涉及原生事件有 “bindtap”
1、流程图
- 双击事件流程图:

2、代码示例
变量记录:
let lastTapTime = 0, //记录上一次点击时间
bindtap事件:
myTap(e) {
// 监听双击事件
let curTime = e.timeStamp
if (lastTapTime > 0) {
if (curTime - lastTapTime < 300) {
console.log("双击屏幕事件");
}
}
lastTapTime = curTime
提示:文章到此结束,文章仅为个人学习记录,若有不足还请大家指出。
边栏推荐
- The log causes these pits in the thread block, you have to prevent
- 仿牛客网项目总结
- Introduction to the basic principles, implementation and problem solving of crawler
- MySQL row locks and gap locks
- 史上超强最常用SQL语句大全
- mysql的行锁和间隙锁
- Win任务栏图标异常解决
- 监听父元素宽高,自适应插件大小
- 【ASWC Arxml结构分解】-7-Explicit(显式)和Implicit(隐式) Sender-Receiver communication描述差异
- Does flinkcdc have any solution for mysql's date field type conversion?
猜你喜欢
随机推荐
Windows taskbar icon abnormal solution
special day to remember
研发过程中的文档管理与工具
POJ1251丛林之路题解
图像基本操作的其他内容
MVVM project development (commodity management system 1)
数据机构----线性表之单向链表
如何使用Photoshop合成星轨照片,夜空星轨照片后期处理方法
Classwork (7) - #598. remainder operation (mod)
阿里三面:MQ 消息丢失、重复、积压问题,该如何解决?
Golang:go模版引擎的使用
史上超强最常用SQL语句大全
Self-made a remote control software - VeryControl
Srping bean in the life cycle
零代码网站开发利器:WordPress
NIO programming
Golang:go开启web服务
JSON 与 JS 对象的区别
LeetCode 415:字符串相加
Image lossless compression software which works: try completely free JPG - C image batch finishing compression reduces weight tools | latest JPG batch dressing tools download








