就是我按下去,然后向下移动,怎么获取我松开手指时这一段的距离?
举个例子:
HTML:
html<!doctype html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Document</title> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <style media="screen"> body { margin: 0; } .test { background-color: blue; width: 100vw; height: 100vh; } </style> </head> <body> <div class="test"></div> </body> </html>
html
<!doctype html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Document</title> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <style media="screen"> body { margin: 0; } .test { background-color: blue; width: 100vw; height: 100vh; } </style> </head> <body> <div class="test"></div> </body> </html>
JavaScript
js (function () { var el = document.querySelector('.test'); var startPosition, endPosition, deltaX, deltaY, moveLength; el.addEventListener('touchstart', function (e) { var touch = e.touches[0]; startPosition = { x: touch.pageX, y: touch.pageY } }); el.addEventListener('touchmove', function (e) { var touch = e.touches[0]; endPosition = { x: touch.pageX, y: touch.pageY } deltaX = endPosition.x - startPosition.x; deltaY = endPosition.y - startPosition.y; moveLength = Math.sqrt(Math.pow(Math.abs(deltaX), 2) + Math.pow(Math.abs(deltaY), 2)); console.log(moveLength); }); })();
js
(function () { var el = document.querySelector('.test'); var startPosition, endPosition, deltaX, deltaY, moveLength; el.addEventListener('touchstart', function (e) { var touch = e.touches[0]; startPosition = { x: touch.pageX, y: touch.pageY } }); el.addEventListener('touchmove', function (e) { var touch = e.touches[0]; endPosition = { x: touch.pageX, y: touch.pageY } deltaX = endPosition.x - startPosition.x; deltaY = endPosition.y - startPosition.y; moveLength = Math.sqrt(Math.pow(Math.abs(deltaX), 2) + Math.pow(Math.abs(deltaY), 2)); console.log(moveLength); }); })();
console.log出来的moveLength就是你要得到的距离。
console.log
moveLength
可以参考Zepto的touch源码:https://github.com/madrobby/zepto/blob/master/src%2Ftouch.js
正好也遇到这个问题。
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
暂无简介
文章 0 评论 0
接受
发布评论
评论(2)
举个例子:
HTML:
JavaScript
console.log
出来的moveLength
就是你要得到的距离。可以参考Zepto的touch源码:https://github.com/madrobby/zepto/blob/master/src%2Ftouch.js
正好也遇到这个问题。