это простой вопрос. Есть ли способ в jQuery или Javascript определить скорость события touchmove? Я использую css, чтобы захватить элемент и сделать его доступным для захвата, это работает нормально, но если я двигаю пальцем быстрее, менее вероятно, что я смогу переместиться на пороговое расстояние, но я намеревался перевернуть страницу, так что есть ли способ определить скорость движения по событию touch move в 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.});
});
спасибо за отличный ответ. выше это модифицированный код. сработало отлично!!!