当前位置:网站首页>小程序更多的手势事件(左右滑动、放大缩小、双击、长按)
小程序更多的手势事件(左右滑动、放大缩小、双击、长按)
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程序设计与应用 2.5 MATLAB运算
- 2022杭电多校第二场1011 DOS Card(线段树)
- 旋度(7)连接失败localhost8080;连接拒绝了
- Why is the lightweight VsCode used more and more?Why eat my C drive 10G?How to Painlessly Clean VsCode Cache?Teach you how to lose weight for C drive
- 安装SQL Server详细教程
- 拳头游戏免版权音乐下载,英雄联盟无版权音乐,可用于视频创作、直播
- 特别数的和
- mysql中添加字段的相关问题
- Datagrip error "The specified database userpassword combination is rejected..."Solutions
- Zero-code website development tool: WordPress
猜你喜欢
Electromagnetic compatibility introductory tutorial (6) test project
支付宝如何生成及配置公钥证书
Dart exception details
电磁兼容简明教程(6)测试项目
滚动条样式修改
我说过无数遍了:从来没有一种技术是为灵活组合这个目标而设计的
Matlab simulink particle swarm optimization fuzzy pid control motor pump
奇葩问题 npm install 报错 gyp ERR
The BP neural network based on MATLAB voice characteristic signal classification
对于升级go1.18的goland问题
随机推荐
Create, modify and delete tables
POJ1287联网题解
The log causes these pits in the thread block, you have to prevent
Matlab simulink particle swarm optimization fuzzy pid control motor pump
插入排序—直接插入排序和希尔排序
头歌MySQL数据库实训答案 有目录
问下 mysql向pg同步多个表的话 有什么好的方案吗?
基于百度OCR的网站验证码在线识别
第02章 MySQL的数据目录【1.MySQL架构篇】【MySQL高级】
目标检测概述-上篇
Three aspects of Ali: How to solve the problem of MQ message loss, duplication and backlog?
2022年牛客多校第四场补题
Golang:go获取url和表单属性值
rhcsa 第四天
三维坐标系距离
C语言学习概览(二)
weight distribution
监听父元素宽高,自适应插件大小
Win任务栏图标异常解决
"By sharing" northwestern university life service | | bytes a second interview on three sides by HR