当前位置:网站首页>小程序更多的手势事件(左右滑动、放大缩小、双击、长按)
小程序更多的手势事件(左右滑动、放大缩小、双击、长按)
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
提示:文章到此结束,文章仅为个人学习记录,若有不足还请大家指出。
边栏推荐
- 太厉害了,终于有人能把文件上传漏洞讲的明明白白了
- MATLAB program design and application of MATLAB 2.5
- VSCode插件推荐(Rust环境)
- weight distribution
- LeetCode每日一题(309. Best Time to Buy and Sell Stock with Cooldown)
- 滚动条样式修改
- Windows taskbar icon abnormal solution
- NIO programming
- "By sharing" northwestern university life service | | bytes a second interview on three sides by HR
- How to generate and configure public key certificate in Alipay
猜你喜欢
NIO编程
Golang: go open web service
特别数的和
Self-made a remote control software - VeryControl
从购买服务器到网站搭建成功保姆级教程~超详细
How JS works
MySQL row locks and gap locks
从底层结构开始学习FPGA(6)----分布式RAM(DRAM,Distributed RAM)
Information system project managers must recite the work of the core test site (56) Configuration Control Board (CCB)
Guest brush SQL - 2
随机推荐
Monitor the width and height of the parent element, adapt to the size of the plug-in
插入排序—直接插入排序和希尔排序
NIO programming
More than 2022 cattle guest school game 4 yue
第02章 MySQL的数据目录【1.MySQL架构篇】【MySQL高级】
CSP-S2019兴奋不已
信息系统项目管理师必背核心考点(五十六)配置控制委员会(CCB)的工作
LeetCode 415:字符串相加
配置我的kitty
Srping bean in the life cycle
特殊的日子,值得纪念
Datagrip error "The specified database userpassword combination is rejected..."Solutions
声音信号处理基频检测和时频分析
LeetCode Question of the Day (309. Best Time to Buy and Sell Stock with Cooldown)
05-SDRAM: Arbitration
Upgrade to heavyweight lock, lock reentrancy will lead to lock release?
阿里云李飞飞:中国云数据库在很多主流技术创新上已经领先国外
2022杭电多校第二场1011 DOS Card(线段树)
crypto-js使用
响应式织梦模板园林景观类网站