在jQuery或Javascript中是否有办法确定触摸移动事件的速度
Is there a way in jQuery or Javascript to determine the speed of a touchmove event?
这是一个简单的问题。在jQuery或Javascript中是否有一种方法可以确定touchmove事件的速度?我使用 css 来抓取元素并使其可抓取,这工作正常,但如果我移动手指的速度更快,我不太可能移动到阈值距离,但我确实打算翻页,所以有没有办法我可以确定 javascript 或 jQuery 中触摸移动事件的移动速度, 我可以将阈值调整为较小的值以补偿速度吗?
var startX, endX, difference, threshold;
var startTime, endTime, timeDiff = 151;
$('#animate')
.bind("touchstart", function (e){
e.preventDefault();
var d = new Date();
startTime = d.getTime();
startX = e.originalEvent.touches[0].pageX; //starting point
})
.bind("touchmove", function (e){
e.preventDefault();
endX =e.originalEvent.changedTouches[0].pageX; //Get the information for finger
difference = startX - endX; //calculate the distance moved.
var moved = minusScreen - difference; //determine the affected css value.
$(this).css("left",moved); //this makes the element moves with my finger.
})
.bind("touchend", function (e) {
var date = new Date();
endTime = date.getTime();
threshold = Math.abs(difference);
timeDiff = endTime - startTime;
if ((threshold > (screenWidth * 0.4)) || (timeDiff < 150)) //make the animation move only when the finger moved more than 30% of the page.
{
if (endX > startX) turnLeft();
else if (endX == startX) {} // havent decide what to do yet
else turnRight();
} else {
$(this).animate({"left": minusScreen}, 100);
}
startX=0; //set the value back to initial.
endX=0; //set the value back to initial.});
});
谢谢你的好答案。 以上是修改后的代码。 效果很好!!
像
这样在触摸开始时获取时间,然后在触摸端再次获取时间
startTime = new Date().getTime()
和endTime = new Date().getTime()
然后计算var speed = abs(endX-startX)/(endTime-startTime)
这是您的整体触摸移动速度(以 PX/ms 为单位)
虽然这是一个老问题,但似乎在触摸事件对象中有一个"timeStamp",所以简单地使用可能更简单、更快捷:
startTime = e.timeStamp();
代替 :
var d = new Date();
startTime = d.getTime();
var endTime 也是如此
相关文章:
- 触摸移动时切换到新元素
- 如何在jquery中找到鼠标滚轮/触摸移动事件的走向
- 如何查找触摸移动到其他对象
- 在jQuery或Javascript中是否有办法确定触摸移动事件的速度
- 连续触摸移动
- 触摸移动和手势更改有什么区别
- 科尔多瓦 - “触摸移动”事件不会立即触发
- HTML5 部分触摸开始触摸移动触摸触摸 iOS
- 在触摸移动时触发时 CSS 过渡不一致
- 获取触摸移动或触摸结束的当前 DOM 对象
- 触摸移动以第二次注册时出现问题
- 触摸移动后触摸端未触发
- 捕获水平触摸移动,同时允许本机垂直滚动
- 需要触摸设备在覆盖另一个元素时保持触摸移动事件触发
- 在 Android 2.3.3 上,触摸移动事件的数量大幅减少
- 如何更改“;触摸移动”;在js中
- 在触摸移动时修改每个触摸的元素
- 触摸移动被卡住忽略尝试取消触摸移动
- 滚动元素上的Javascript触摸移动事件
- Opacity获胜'在iOS设备上滚动或触摸移动事件时,不要设置动画