JavaScript监听触摸事件代码实例(2)

<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title></title> </head> <body> <!--利用touchstart和touchend触摸前后监听到的四个坐标分别是触摸前的x,y坐标和触摸后的x,y坐标, 然后用数学公式进行运算得出方向--> </body> <script type="text/javascript"> document.getElementsByTagName('body')[0].addEventListener('touchstart', function(e) { touch.startY = e.targetTouches[0].pageY; touch.startX = e.targetTouches[0].pageX; //console.log("点击时的X坐标" + nStartX + "和Y坐标" + nStartY); }); document.getElementsByTagName('body')[0].addEventListener('touchmove', function(e) { touch.whenChangY = e.changedTouches[0].pageY; touch.whenChangX = e.changedTouches[0].pageX; //console.log("滑动时的X坐标" + nWhenChangX + "和Y坐标" + nWhenChangY); }) document.getElementsByTagName('body')[0].addEventListener('touchend', function(e) { touch.changY = e.changedTouches[0].pageY; touch.changX = e.changedTouches[0].pageX; //console.log("滑动后的X坐标" + nChangX + "和Y坐标" + nChangY); var swDirection = direction(touch.startX, touch.changX, touch.startY, touch.changY); </script> </html>

GitHub地址:https://github.com/lianglixiong

内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:http://www.heiqu.com/84561654878359cc522f3c48def01e63.html