Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/70.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在jQuery或Javascript中是否有方法确定touchmove事件的速度?_Javascript_Jquery_Performance_Touch Event_Touchmove - Fatal编程技术网

在jQuery或Javascript中是否有方法确定touchmove事件的速度?

在jQuery或Javascript中是否有方法确定touchmove事件的速度?,javascript,jquery,performance,touch-event,touchmove,Javascript,Jquery,Performance,Touch Event,Touchmove,这是一个简单的问题。 在jQuery或Javascript中是否有方法确定touchmove事件的速度? 我使用css抓取元素并使其可抓取,这很好,但如果我移动手指的速度更快,我移动到阈值距离的可能性就更小,但我确实打算翻页,所以有没有一种方法可以确定javascript或jQuery中触摸移动事件的移动速度,我可以把阈值调整到一个较小的值来补偿速度 var startX, endX, difference, threshold; var startTime, endTime, tim

这是一个简单的问题。 在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.});
    });
var startX、endX、差值、阈值;
变量startTime、endTime、timeDiff=151;
$(“#设置动画”)
.bind(“touchstart”,函数(e){
e、 预防默认值();
var d=新日期();
startTime=d.getTime();
startX=e.originalEvent.touchs[0].pageX;//起点
})
.bind(“触摸移动”,函数(e){
e、 预防默认值();
endX=e.originalEvent.changedTouches[0].pageX;//获取finger的信息
difference=startX-endX;//计算移动的距离。
var moved=minusScreen-difference;//确定受影响的css值。
$(this.css(“left”,moved);//这使元素用我的手指移动。
})
.bind(“touchend”),函数(e){
变量日期=新日期();
endTime=date.getTime();
阈值=Math.abs(差值);
timeDiff=结束时间-开始时间;
if((阈值>(屏幕宽度*0.4))| |(timeDiff<150))//仅当手指移动超过页面的30%时才移动动画。
{           
如果(endX>startX)左转();
否则,如果(endX==startX){}//还没有决定做什么
否则右转();
}否则{
$(this.animate({“left”:minusScreen},100);
}
startX=0;//将该值设置回初始值。
endX=0;//将值设置回初始值。});
});

谢谢你的回答。以上是修改后的代码。工作得很好

在touchstart上获取时间,然后再像这样在touchend上获取时间

startTime=newdate().getTime()
endTime=newdate().getTime()


然后计算
var speed=abs(endX startX)/(endTime startTime)
这是您现在的整体触摸移动速度,单位为px/ms

虽然这是一个老问题,但触摸事件对象中似乎有一个“时间戳”,因此简单使用它可能更简单、更快:

startTime = e.timeStamp();
代替:

var d = new Date();      
startTime = d.getTime();

var endTime的情况也一样

在touchstart和touchend上计算时间
abs(endX startX)/endTime startTime
这是您的整体触摸移动速度,以px为单位/ms@jakee似乎合法。将其作为答案发布尝试,修改代码,但其不起作用。这两个值似乎在触摸结束时同时给出。结果是两个相同的时间值。我不想说,但是你初始化一个日期,然后两次请求它的时间。。。转到
startTime=new Date().getTime()
endTime=new Date().getTime()
是,工作正常。非常感谢。稍后我会做一些整理工作。
e.timeStamp
是一个
DOMHighResTimeStamp
DOMTimeStamp
,而不是一个
函数
,在Firefox上它返回的是纳秒,而不是自2004年以来一直打开的毫秒。直到最近,Android或Chrome才支持它。使用
Date.now()