移動端觸控
阿新 • • 發佈:2018-12-06
var div=document.querySelector("div"); var startX,startY,moveX,moveY,distanceX,distanceY; // 開始觸控 div.addEventListener("touchstart",function(e){ startX=e.targetTouches[0].clientX; startY=e.targetTouches[0].clientY; }) // 觸控滑動 持續觸發 div.addEventListener("touchmove",function(e){ moveX=e.targetTouches[0].clientX; moveY=e.targetTouches[0].clientY; distanceX=moveX-startX; distanceY=moveY-startY; div.style.transform="translate("+distanceX+"px,"+distanceY+"px)"; }); // 觸控結束 div.addEventListener("touchend",function(e){ })