javascript - 如何区分滚动事件和长按事件?
问题描述
问题 :
SomeDomElement.addEventListener('touchstart', function preventLongPress(event) {
if (event.touches.length >=1) event.preventDefault();
}, false);
如果我使用 : if (event.touches.length >=1) event.preventDefault();
那么这可以防止长按事件但也会禁用滚动事件。
长按没有touchmove
或touchend
事件。
我想要什么:
防止长按但不防止滚动
注意:我只使用 vanilla Javascript,没有jQuery
解决方案
希望这会帮助你。
document.addEventListener("touchstart", function(){
detectTap = false;
});
document.addEventListener("touchmove", function(){
detectTap = true;
});
document.addEventListener("touchend", function(){
if(detectTap)
alert("scrolled"); /* here add whatever functionality you wants */
else
alert("long pressed"); /* here add whatever functionality you wants */
});
推荐阅读
- android - kotlin Android 微调器 notifyDataSetChanged()
- spring - 添加bean后出错
- android - 通过 multipart 将 byte[] 图像上传到服务器
- python-2.7 - 如何以完全隐藏的方式运行exe文件
- python - 列中具有多个标签的一种热编码
- botframework - 从 iFrame 中的 ChatBot URL 获取动态令牌
- hibernate - 如何获取列中值所在的记录数
(字符串)在休眠? - ios - 检测设备是否正在充电或已拔出
- testing - 如何在不同设备上测试移动应用
- html - 如何将 css flex 概念应用于角度材料