在HTML5中获得Touchmove的速度

时间:2013-03-01 06:49:52

标签: android html html5

如何在HTML5中获得我的手指从一个点移动到另一个点的速度(velocityX)。我在android中使用了速度(velocityX)。像这样

public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY){

    private static final int SWIPE_MIN_DISTANCE = 30;
private static final int SWIPE_MAX_OFF_PATH = 200;
private static final int SWIPE_THRESHOLD_VELOCITY = 400;


try {
    if (Math.abs(e1.getY() - e2.getY()) > SWIPE_MAX_OFF_PATH)
        return false;
    // right to left swipe
    if(e1.getX() - e2.getX() > SWIPE_MIN_DISTANCE && Math.abs(velocityX) > SWIPE_THRESHOLD_VELOCITY) {

        startActivity( new Intent( this, x.class ) );
    }  else if (e2.getX() - e1.getX() > SWIPE_MIN_DISTANCE && Math.abs(velocityX) > SWIPE_THRESHOLD_VELOCITY) {
        //  Toast.makeText(getApplicationContext(), "Right Swipe", Toast.LENGTH_SHORT).show();

        startActivity( new Intent( this , aasas.class) );




    }
    else if(e1.getY() - e2.getY() > SWIPE_MIN_DISTANCE && Math.abs(velocityX) > SWIPE_THRESHOLD_VELOCITY) {

    }  else if (e2.getY() - e1.getY() > SWIPE_MIN_DISTANCE && Math.abs(velocityX) > SWIPE_THRESHOLD_VELOCITY) {

    }
} catch (Exception e) {
    // nothing
}

return true;

}

我希望在html5中实现相同的功能,但是我无法获得velocityX和我的html5代码就是这个

      this.downX = null;
   this.upX = null;

document.body.addEventListener('touchstart', function(event) {
        event.preventDefault();
        console.log("movvv");
                this.downX = event.touches[0].pageX;

    }, true);

    document.body.addEventListener('touchmove', function(event) {
        event.preventDefault();
        this.upX = event.touches[0].pageX;
    }, true);


    document.body.addEventListener('touchend', function(event) {
        event.preventDefault();

        if ((this.downX - this.upX) > 50) {

            console.log("goooo");

        }
        if ((this.upX - this.downX) > 50) {

        }


    }, true);

其实我想把这行Android改为Html5

if(e1.getX() - e2.getX() > SWIPE_MIN_DISTANCE && Math.abs(velocityX) > SWIPE_THRESHOLD_VELOCITY)

如果有任何PLZ指南,如果HTML5中的单个Touch有任何功能,如Android onSingleTapUp

0 个答案:

没有答案