Touch触摸事件详解
element.addEventListener("touchstart",function(e){})
element.addEventListener("touchend",function(e){})
element.addEventListener("touchmove",function(e){})
element.addEventListener("touchcancel",function(e){})
- 由于某个事件取消了触摸:例如触摸过程被一个模态的弹出框打断。
- 触点离开了文档窗口,而进入了浏览器的界面元素、插件或者其他外部内容区域。
- 当用户产生的触点个数超过了设备支持的个数,从而导致 TouchList 中最早的 Touch 对象被取消。
event.touches
event.targetTouches
event.changeTouches
$(element).mousemove(function(e) { var xx = e.originalEvent.x || e.originalEvent.layerX || 0; var yy = e.originalEvent.y || e.originalEvent.layerY || 0; });
$(element).mousemove(function(e) { var xx=e.pageX; var yy=e.pageY; });
<canvas id="canvas" style="border:1px solid #000;background:#666"></canvas> <script> var canvas=document.getElementById("canvas"); var wWith=window.innerWidth; var wHeight=window.innerHeight; canvas.width=wWith; canvas.height=wHeight; var cxt=canvas.getContext("2d"); cxt.strokeStyle="#fff";
cxt.lineJoin="round"; cxt.lineWidth="10"; var sX,sY; canvas.addEventListener("touchstart",function(e){ e.preventDefault(); if (e.targetTouches.length == 1) { var touch = e.targetTouches[0]; sX=Math.floor(touch.pageX)-Math.floor(canvas.offsetLeft); sY=Math.floor(touch.pageY)-Math.floor(canvas.offsetTop); } cxt.moveTo(sX,sY); }); canvas.addEventListener("touchmove",function(e){ var e=e || window.event; var touch = e.targetTouches[0]; var x=Math.floor(touch.pageX)-Math.floor(canvas.offsetLeft); var y=Math.floor(touch.pageY)-Math.floor(canvas.offsetTop); cxt.lineTo(x,y); cxt.stroke(); sX=x; sY=y; }); canvas.addEventListener("touchend",function(e){ }); </script>