-
TouchMove
2020-12-26 10:01:36Touchmove event" doesn't work for me, but "mousemove" and "touchdown" works fine. Can anyone come across a similar problem?</p><p>该提问来源于开源项目:Mapsui/Mapsui</p>... -
触摸事件 touchstart、touchmove、touchend
2018-10-22 11:11:13目录 触摸事件 触摸事件编码 触摸手指个数分析 触摸目标 DOM 元素分析 触摸位置分析 触摸事件 HTML5 中, PC 端基于鼠标的界面互动主要是单击, 移动端界面交互方式主要是触摸。... touchmove ...目录
触摸事件概述
1、HTML5 中, PC 端基于鼠标的界面互动主要是单击, 移动端界面交互方式主要是触摸。
2、移动端浏览器触摸事件:
事件名称 描述 是否包含 touches 数组 touchstart 触摸开始,多点触控,后面的手指同样会触发 是 touchmove 接触点改变,滑动时 是 touchend 触摸结束,手指离开屏幕时 是 touchcancel 触摸被取消,当系统停止跟踪触摸的时候触发 否 3、每个触摸事件都包括了三个触摸列表,每个列表里包含了对应的一系列触摸点(用来实现多点触控):
1)touches:当前位于屏幕上的所有手指的列表。
2)targetTouches:位于当前DOM元素上手指的列表。
3)changedTouches:涉及当前事件手指的列表。4、每个 Touch 对象包含的属性如下:
clientX:触摸目标在视口中的x坐标。
clientY:触摸目标在视口中的y坐标。
identifier:标识触摸的唯一ID。
pageX:触摸目标在页面中的x坐标。
pageY:触摸目标在页面中的y坐标。
screenX:触摸目标在屏幕中的x坐标。
screenY:触摸目标在屏幕中的y坐标。
target:触摸的DOM节点目标。触摸事件编码
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <!-- meta使用viewport以确保页面可自由缩放 --> <meta name="viewport" content="width=device-width, initial-scale=1"> <title>触摸事件</title> <!-- 引入 jQuery 库 --> <script src="http://apps.bdimg.com/libs/jquery/1.10.2/jquery.min.js"></script> <script type="text/javascript"> $(function () { //手指触摸屏幕时触发事件 $('body').on('touchstart', function () { $('p').css({'color': '#f00'}); console.log("用户手指触摸到屏幕..."); }); //手指在屏幕上移动时触发 $('body').on('touchmove', function (even) { $('p').css({'color': '#0f0'}); console.log("用户手指在屏幕上移动..."); }); //手指离开屏幕时触发 $('body').on('touchend', function () { $('p').css({'color': '#00f'}); console.log("用户手离开屏幕..."); }); }); </script> </head> <body> <p> 工人阶级是我国的领导阶级,是全面建成小康社会、坚持和发展中国特色社会主义的...<br> 在中国工会第十七次全国代表大会即将召开之际,让我...<br> 工人阶级是我们党最坚实最可靠的阶级基础...<br> 我国工人阶级是我们党最坚实最可靠的阶级基础。我国工人阶级从来都具有走在...<br> 工人阶级和广大劳动群众始终是推动我国经济社会发展、维护社会安定团结的根本...<br> 改革开放以来,我国工人阶级队伍不断壮大,素质全面提高,结构更加优化,面貌焕然...<br> </p> </body> </html>
1、如果在 PC 上访问,可以使用 Chrome 浏览器的移动响应式设备进行模拟,或者直接在移动设备(如手机)上访问
2、Chrome 浏览器,F12 进入开发者模式,然后点击左上角的第二个按钮进行切换。
3、上面是使用 JQuery 的写法,推荐使用如下所示的 JavaScript 方式,因为在获取回调函数的 事件对象时,JQuery 方式会有问题,JavaScript 则是没有问题的。
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <!-- meta使用viewport以确保页面可自由缩放 --> <meta name="viewport" content="width=device-width, initial-scale=1"> <title>触摸事件</title> <!-- 引入 jQuery 库 --> <script src="http://apps.bdimg.com/libs/jquery/1.10.2/jquery.min.js"></script> <script type="text/javascript"> /** * element.addEventListener(event, function, useCapture):向指定元素添加事件句柄 * useCapture:true - 事件句柄在捕获阶段执行;false(默认) - 事件句柄在冒泡阶段执行 */ document.addEventListener('touchstart', touch, false); document.addEventListener('touchmove', touch, false); document.addEventListener('touchend', touch, false); function touch(event) { /**兼用 IE 浏览器*/ var event = event || window.event; var oInp = document.getElementById("inp"); switch (event.type) { case "touchstart": oInp.innerHTML = "Touch started (" + event.touches[0].clientX + "," + event.touches[0].clientY + ")"; break; case "touchend": oInp.innerHTML = "<br>Touch end (" + event.changedTouches[0].clientX + "," + event.changedTouches[0].clientY + ")"; break; case "touchmove": oInp.innerHTML = "<br>Touch moved (" + event.touches[0].clientX + "," + event.touches[0].clientY + ")"; break; } } </script> </head> <body> <p id="inp"> </p> </body> </html>
4、主要就是在绑定事件的部分略有不同,可以参考 HTML DOM addEventListener() 方法
触摸手指个数分析
1、如下所示,将屏幕上触摸的手指个数信息打印出来进行分析。
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <!-- meta使用viewport以确保页面可自由缩放 --> <meta name="viewport" content="width=device-width, initial-scale=1"> <style type="text/css" rel="stylesheet"> .start { border: 1px solid red; margin-top: 5px; } .move { border: 1px solid green; margin-top: 5px; overflow: auto; } .end { border: 1px solid blue; margin-top: 5px; } </style> <title>触摸事件</title> <!-- 引入 jQuery 库 --> <script src="http://apps.bdimg.com/libs/jquery/1.10.2/jquery.min.js"></script> <script type="text/javascript"> $(function () { //$(".move")[0].addEventListener("touchstart", function (event) { document.addEventListener("touchstart", function (event) { var touchesSize = event.touches.length; var targetTouchesSize = event.targetTouches.length; var changedTouchesSize = event.changedTouches.length; $(".start").append("tSize=" + touchesSize + ",targetTSize=" + targetTouchesSize + ",changedTSize=" + changedTouchesSize + "\r\n"); }); //$(".move")[0].addEventListener("touchstart", function (event) { document.addEventListener("touchmove", function (event) { var touchesSize = event.touches.length; var targetTouchesSize = event.targetTouches.length; var changedTouchesSize = event.changedTouches.length; $(".move").append("tSize=" + touchesSize + ",targetTSize=" + targetTouchesSize + ",changedTSize=" + changedTouchesSize + "\r\n"); }); //$(".move")[0].addEventListener("touchstart", function (event) { document.addEventListener("touchend", function (event) { var touchesSize = event.touches.length; var targetTouchesSize = event.targetTouches.length; var changedTouchesSize = event.changedTouches.length; $(".end").append("tSize=" + touchesSize + ",targetTSize=" + targetTouchesSize + ",changedTSize=" + changedTouchesSize + "\r\n"); }); }); </script> </head> <body> <div class="start"> <b>touchstart</b> </div> <div class="move"> <b>touchmove</b> </div> <div class="end"> <b>touchend</b> </div> </body> </html>
2、如下所示,左图是 document.addEventListener、右图是 $(".move")[0].addEventListener。
3、tSize 是当前位于屏幕上的所有手指的列表个数、targetTSize 是位于当前绑定事件的 DOM 元素上手指的列表个数、changedTSize 是涉及当前事件手指的列表个数。
4、对于各个数据的含义,想必测试之后一目了然,就不再多说了。
触摸目标 DOM 元素分析
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <!-- meta使用viewport以确保页面可自由缩放 --> <meta name="viewport" content="width=device-width, initial-scale=1"> <style type="text/css" rel="stylesheet"> html, body { width: 100%; height: 100%; } .testArea1 { width: 100%; height: 15%; background-color: #1b6d85; } .testArea2 { width: 100%; height: 15%; background-color: #1b6d85; } .start { border: 1px solid red; margin-top: 5px; } .move { border: 1px solid green; margin-top: 5px; overflow: auto; } .end { border: 1px solid blue; margin-top: 5px; } </style> <title>触摸事件</title> <!-- 引入 jQuery 库 --> <script src="http://apps.bdimg.com/libs/jquery/1.10.2/jquery.min.js"></script> <script type="text/javascript"> /**阻止浏览器默认右键点击事件*/ $(document).bind("contextmenu", function () { console.log("用户点击鼠标右键....." + new Date().getTime()); return false; }); $(function () { /**注意调用的方法不要加括号()*/ $(".testArea1")[0].addEventListener("touchstart", touchstartFun); $(".testArea1")[0].addEventListener("touchmove", touchmoveFun); $(".testArea1")[0].addEventListener("touchend", touchendFun); $(".testArea2")[0].addEventListener("touchstart", touchstartFun); $(".testArea2")[0].addEventListener("touchmove", touchmoveFun); $(".testArea2")[0].addEventListener("touchend", touchendFun); }); /** * 手指触摸屏幕时——函数调用 * @param event */ function touchstartFun(event) { /**event.targetTouches[0]:表示获取发生在当前 DOM 元素第一个手指对象 * target:手指触摸的 DOM节点,对原生 JS 不熟悉时,使用 $()转为 JQuery 对象操作 * */ console.log("start——" + event.targetTouches[0].target.innerHTML);//js获取触摸DOM对象的文本值 console.log("start——" + $(event.targetTouches[0].target).attr("class"));//JQuery 获取触摸对象的class属性值 } /** * 手指在触摸屏上移动时——函数调用 * @param event */ function touchmoveFun(event) { /**event.targetTouches[0]:表示获取发生在当前 DOM 元素第一个手指对象 * target:手指触摸的 DOM节点,对原生 JS 不熟悉时,使用 $()转为 JQuery 对象操作 * */ console.log("move——" + event.targetTouches[0].target.innerHTML);//js获取触摸DOM对象的文本值 console.log("move——" + $(event.targetTouches[0].target).attr("class"));//JQuery 获取触摸对象的class属性值 } /** * 手指离开屏幕时——函数调用 * @param event */ function touchendFun(event) { /**event.changedTouches[0]:表示获取当前 DOM 元素发生此事件的第一个手指对象 * target:手指触摸的 DOM节点,对原生 JS 不熟悉时,使用 $()转为 JQuery 对象操作 * * 注意:离开时应该是获取 changedTouches,而不是 targetTouches、touches,因为当手指全部离开屏幕时,它们个数可能为0 * */ console.log("end————" + event.changedTouches[0].target.innerHTML);//js 获取触摸 DOM 对象的文本值 console.log("end————" + $(event.changedTouches[0].target).attr("class"));//JQuery 获取触摸对象的class属性值 } </script> </head> <body> <div class="testArea1"> 触摸测试区域1 </div> <div class="start"> <b>touchstart</b> </div> <div class="move"> <b>touchmove</b> </div> <div class="end"> <b>touchend</b> </div> <div class="testArea2"> 触摸测试区域2 </div> </body> </html>
触摸位置分析
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <!-- meta使用viewport以确保页面可自由缩放 --> <meta name="viewport" content="width=device-width, initial-scale=1"> <style type="text/css" rel="stylesheet"> html, body { width: 100%; height: 100%; } .testArea1 { width: 100%; height: 15%; background-color: #1b6d85; } .testArea2 { width: 100%; height: 15%; background-color: #1b6d85; } .start { border: 1px solid red; margin-top: 5px; } .move { border: 1px solid green; margin-top: 5px; overflow: auto; } .end { border: 1px solid blue; margin-top: 5px; } </style> <title>触摸事件</title> <!-- 引入 jQuery 库 --> <script src="http://apps.bdimg.com/libs/jquery/1.10.2/jquery.min.js"></script> <script type="text/javascript"> /**阻止浏览器默认右键点击事件*/ $(document).bind("contextmenu", function () { console.log("用户点击鼠标右键....." + new Date().getTime()); return false; }); $(function () { /**注意调用的方法不要加括号()*/ $(".testArea1")[0].addEventListener("touchstart", touchstartFun); $(".testArea1")[0].addEventListener("touchmove", touchmoveFun); $(".testArea1")[0].addEventListener("touchend", touchendFun); $(".testArea2")[0].addEventListener("touchstart", touchstartFun); $(".testArea2")[0].addEventListener("touchmove", touchmoveFun); $(".testArea2")[0].addEventListener("touchend", touchendFun); }); /** * 手指触摸屏幕时——函数调用 * @param event */ function touchstartFun(event) { /**event.targetTouches[0]:表示获取发生在当前 DOM 元素第一个手指对象 * target:手指触摸的 DOM节点,对原生 JS 不熟悉时,使用 $()转为 JQuery 对象操作 * */ var lentgX = event.targetTouches[0].clientX; var lentgY = event.targetTouches[0].clientY; $(".start").append("<br>" + lentgX + "——" + lentgY); } /** * 手指在触摸屏上移动时——函数调用 * @param event */ function touchmoveFun(event) { /**event.targetTouches[0]:表示获取发生在当前 DOM 元素第一个手指对象 * target:手指触摸的 DOM节点,对原生 JS 不熟悉时,使用 $()转为 JQuery 对象操作 * */ var lentgX = event.targetTouches[0].clientX; var lentgY = event.targetTouches[0].clientY; $(".move").append("<br>" + lentgX + "——" + lentgY); } /** * 手指离开屏幕时——函数调用 * @param event */ function touchendFun(event) { /**event.changedTouches[0]:表示获取当前 DOM 元素发生此事件的第一个手指对象 * target:手指触摸的 DOM节点,对原生 JS 不熟悉时,使用 $()转为 JQuery 对象操作 * * 注意:离开时应该是获取 changedTouches,而不是 targetTouches、touches,因为当手指全部离开屏幕时,它们个数可能为0 * */ var lentgX = event.changedTouches[0].clientX; var lentgY = event.changedTouches[0].clientY; $(".end").append("<br>" + lentgX + "——" + lentgY); } </script> </head> <body> <div class="testArea1"> 触摸测试区域1 </div> <div class="start"> <b>touchstart</b> </div> <div class="move"> <b>touchmove</b> </div> <div class="end"> <b>touchend</b> </div> <div class="testArea2"> 触摸测试区域2 </div> </body> </html>
-
touchmove event not fired
2020-12-02 01:32:20t catch touchmove event then I use d3.zoom in v4. If I disable zoom everything is ok. With mouse events also everything is ok.</p><p>该提问来源于开源项目:d3/d3-zoom</p></div> -
touchmove事件详释
2020-01-09 00:36:32touchmove事件详释 用这个代码测试 <style> *{ padding:0; margin:0; } div{ height:100px; width:100px; background:liner-gradient(to right,red,blue); } </style> <body> <div>...touchmove事件详释
用这个代码测试
<style> *{ padding:0; margin:0; } div{ height:100px; width:100px; background:liner-gradient(to right,red,blue); } </style> <body> <div>1</div> <div>2</div> </body>
var div=document.querySelector('div'); //开始触摸 div.addEventListener('touchstart',function(e){ console,log(e.touches); });
1,记录手指的起始位置–坐标
2.记录手指离开屏幕时的坐标值–记录手指在滑动过程中的坐标值
3.计算两个记录的手指坐标的差异
4.让dom元素也进行相应数值的偏移touches: 是指当前屏幕所有的手指对象
targetTouches: 当前元素上的手指对象
changedTouches: 当前屏幕上变换的手指对象–从无到有,从有到无*targetTouches与touches在测试中没有区别
手指对象的坐标
screenX/screenY:是手指的触摸点相对于屏幕左上角的坐标距离
clientX/clientY:相对于当前视口–移动端屏幕
pageX/pageY:相对于当前页面的内容–会有滚动条–包含滚动的//拖拽操作--drag var div=document.querySelector('div'); var startX,startY,moveX,moveY,distanceX,distanceY; //开始触摸 //如果把div换成document事件,那么就能自动捕获到当前响应事件的对象,用的是e.target属性 div.addEventListener('touchstart',function(e){ startX=e.targetTouches[0].clientX; startY=e.targetTouches[0].clientY; }); //触摸滑动~持续 div.addEventListener('touchmove',function(e){ //记录手指滑动过程的坐标值 moveX=e.targetTouches[0].moveX; moveY=e.targetTouches[0].moveY; //计算与上一次坐标的差异 distanceX=moveX-startX; distanceY=moveY-startY; //设置偏移 //如果把div换成document事件,这里的div要换成e.target div.style.transform='translate('+distanceX+'px',"+distanceY+'px')'; }); //触摸结束 div.addEventListener('touchend',function(e){ })
-
Touchmove should trigger filter method
2020-12-02 04:27:35<div><p>Similarly to touchstarted we want to have the ability to apply a filter to touchmove events. This way we can programmatically allow developers to filter touchmove events instead of allowing ... -
touchmove A Vue.js project Build Setup # install dependencies npm install # serve with hot reload at localhost:8080 npm run dev # build for production with minification npm run build # build for...
-
Vue 绑定使用 touchstart touchmove touchend
2019-09-05 14:39:27Vue 简单绑定 touchstart touchmove touchend今天要做一个页面div长按后触发事件,简单学习后实现如下:
先看代码:<template> <div> <div class="test" @touchstart="gtouchstart()" @touchmove="gtouchmove()" @touchend="gtouchend()">试一试呀!</div> </div> </template> <script> export default { data () { return { } }, methods:{ gtouchstart(){ window.console.log('1,按下啦啦啦啦啦') }, gtouchmove(){ window.console.log('2,按下并且在移动呢') }, gtouchend(){ window.console.log('3,松开啦啦啦啦啦') } } } </script> <style scoped> .test{ width: 100%; height: 50px;; text-align: center; background-color: red; line-height: 50px; font-size: 50px; } </style>
看结果:
鼠标在红色区域内按下会输出1,
按下鼠标不松开然后移动会输出2,
松开后就会输出3,根据自己的情况在三个函数里写入相应的功能。
-
Fix for android touchmove bug
2020-12-08 19:04:20<p>This fix adds a preventDefault() to the touchmove event when the user is not trying to scroll vertically. <p>The bug is not possible to reproduce on android emulator (don't know why :confused: ... -
Dispatch click when touchmove was cancled
2020-11-25 17:57:04<div><p>When touchmove event was cancled by evt.preventDefault() clicks don't work anymore with Apple Pencil. So I check if this happend and dispatch a new click-event on touchEnd.</p><p>该提问... -
移动端touchmove(genCenter)问题
2021-01-03 09:53:05<div><h2>[BUG 反馈] 移动端touchmove问题 浏览器版本号 <p>Chrome moblie模式 iphone 63.0.3239.108 <h3>Vue 版本号 <p>2.5.13 组件库版本号 <p>0.19.0 现象描述 <pre><code> 移动端touchmove无法实时的获取地图... -
3d 云标签,支持mousemove touchmove自有惯性滚动
2019-01-24 12:46:073d 云标签,支持mousemove touchmove自有惯性滚动。 -
Power Select is activated on touchmove
2020-12-26 13:37:30<div><p>On both iOS and Android, if you begin a touchmove event on a Power Select, the Power Select is focussed and, if it is a Power Select Multiple, it also opens its options list. This has the ... -
滑动touchmove touchend遇到的问题
2020-02-12 18:21:59今天遇到一个问题,在优化touchmove时候使用了防抖,touchmove和touchend中都改变了元素的样式,问题就产生了。 函数节流和防抖都是使用setTimeout做延时,当拖动动作完成之后会触发touchend,touchmove有延时,可能... -
touchmove Bug 工作遇到
2016-01-27 13:34:00touchmove在安卓浏览器上只会触发一次,需要preventDefault() touchmove events in Android web browsers have a really serious bug. If you don't include the following code, the touchmove event will fire ... -
[Intervention] Ignored attempt to cancel a touchmove event with cancelable=false, for example becaus
2018-09-02 10:41:25[Intervention] Ignored attempt to cancel a touchmove event with cancelable=false, for example because scrolling is in progress and cannot be interrupted 如何解决 使用css3 touch-action: non... -
Touchmove (iOS) stops animation loop
2021-01-12 08:43:01<div><p>After sliding on iOS (touchmove), the animation loop stops regardless pauseOnAction or animationLoop settings. After sliding left or right the animation should resume.</p><p>该提问来源于开源... -
dom touchmove event is broken by the plugin
2020-12-09 12:10:56It works well except one important issue : touchmove never calls touchend once we lift our finger from the screen. This makes the next touchmove to not start on DOM side. We have to tap once (and so ... -
JavaScript touch 事件 touchstart touchmove touchend
2020-12-02 10:25:29JavaScript touch 事件 touchstart touchmove touchend MDN 官方文档: https://developer.mozilla.org/en-US/docs/Web/API/Touch_events 一、touch 事件有哪些 页面中的 touch 事件一般在移动端使用,pc 端是没有... -
H5页面开发的touchmove事件
2019-09-24 14:00:28在做一屏滚动的H5页面的时候,必须移除touchmove事件,如果不移除,在安卓机上会触发微信原生的向下滚动拉出刷新。在IOS上出现上下都可以继续滑动,所以需要移除document的touchmove事件。 $(document).on('... -
HTML5触摸事件(touchstart、touchmove和touchend)的实现
2020-11-20 13:37:10今天为大家介绍的事件主要是触摸事件:touchstart、touchmove和touchend。 一开始触摸事件touchstart、touchmove和touchend是iOS版Safari浏览器为了向开发人员传达一些信息新添加的事件。因为iOs设备既没有鼠标也... -
移动端触摸事件touchmove的坑
2019-05-03 23:19:23一说到移动端触摸事件,大家的反应不就是touchstart,touchmove,touchend吗,相当于pc段的mousedown,mousemove,mouseup,只要是写过pc端的js的话,若不慎,往往会坑在移动端。 比如说最近我就被touchmove坑了... -
Added touchmove event for Titanium for Android.
2021-01-06 12:41:34<div><p>Hi, <p>This commit has added the touchmove event for the ScrollableView for Android. More events to come.</p><p>该提问来源于开源项目:appcelerator/titanium_mobile</p></div> -
移动端浏览器触摸事件@touchstart=“touchstart“ @touchend=“touchend“ @touchmove=“touchmove
2020-09-12 20:33:12touchstart 触摸开始,多点触控,后面的手指同样会触发 touchmove 接触点改变,滑动时 touchend 触摸结束,手指离开屏幕时 -
click和touchmove vue_移动端touch事件影响click事件以及在touchmove添加preventDefault导致页面无法滚动的...
2020-12-22 15:29:23这两天自己在写一个手机网页,用到了触屏滑动的...之后百度了一下这个问题,原因是主要是由于200ms超时导致内核不一定会一直处理touchmove事件,一旦超时会将后续所有的事件转交给UI处理,导致touchmove不会一直触... -
TouchMove intermediate points are not DPI aware
2021-01-12 08:04:44Using the TouchMove event in a multi-dpi scenario, the interpolated points returned are not correctly adjusting for DPI, whereas the current point is. When using the event to draw a path, you can see ... -
touchmove和touchend的使用
2019-09-28 00:44:52touchmove:当手指在屏幕上滑动时连续的触发。在这个事件发生期间,调用preventDefault()可阻止滚动。touchend:当手指从屏幕上移开时触发。touchcancel:当系统停止跟踪触摸时触发。关于此事件的确切触发事件,文档中... -
touchmove事件中禁止滚动
2018-08-13 14:55:35今天写一个简单的react轮播插件,发现touchmove的preventDefualt()无效,且chrome浏览器给出下面提示: react-dom.development.js:1458 [Intervention] Unable to preventDefault inside passive event listener ... -
TouchMove.vue
2020-07-15 10:38:37自己封装的微信小程序拖动窗口组件. -
Touchmove events not being sent to HTML ad content
2020-12-28 10:40:17d like to use touchmove in order to build rich playable ads that use swipe and other touch gestures besides simple tapping. <p>For what it's worth, this works well on the iOS SDK. <ul><li>[x] I am... -
微信touchmove不生效
2016-06-07 14:09:00最近在写一个微信里面滑动切换图片的功能,发现在...touchmove只触发了一次。 解决方案: 在touchstart里面添加e.preventDefault(); ```javascript var ul = document.querySelector('ul'); ul.addEventLi...