检测触摸设备的左/右滑动,但允许上/下滚动。

16 浏览
0 Comments

检测触摸设备的左/右滑动,但允许上/下滚动。

我需要检测和响应左/右划动,但希望给用户在同一元素上滚动的能力,只要他的手指只是左/右移动且上/下最大距离为X像素,就不应该滚动,但当他超过X时,应该滚动。

所以我做的是:

var startX, startY, $this = $(this);
function touchmove(event) {
    var touches = event.originalEvent.touches;
    if (touches && touches.length) {
        var deltaX = touches[0].pageX - startX;
        var deltaY = touches[0].pageY - startY;
        if (Math.abs(deltaY) > 50) {
            $this.html('X: ' + deltaX + ' Y: ' + deltaY + 'TRUE');
            $this.unbind('touchmove', touchmove);
            return true;
        } else {
            $this.html('X: ' + deltaX + ' Y: ' + deltaY);
            event.preventDefault();
        }
    }
}
function touchstart(event) {
    var touches = event.originalEvent.touches;
    if (touches && touches.length) {
        startX = touches[0].pageX;
        startY = touches[0].pageY;
        $this.bind('touchmove', touchmove);
    }
    //event.preventDefault();
}

但在“if”情况下,它并没有恢复滚动的功能...

谢谢任何建议。

0