【问题标题】:JQuery-UI horizontal dragging with vertical scrolling带有垂直滚动的JQuery-UI水平拖动
【发布时间】:2014-04-01 01:38:32
【问题描述】:

我有一页只有水平方向(在 X 轴上)的可拖动 div。

当我在触摸设备中时,由于滚动和拖动之间的冲突,我无法向下滚动页面。

这是jsfiddle example(在触摸设备中测试并尝试滚动)。

我的可拖动代码:

 $(".cell").draggable({
    axis: "x",
    drag: function (event, ui) {
        var size = $(".cell").width();

        if (ui.offset.left > size - em(4)) {
            ui.position.left = size - em(4);
        }
    },
    start: function (event, ui) {
        if (initialPosition == null) {
            initialPosition = ui.position.left;
        }

        $(".cell").not(ui.helper).each(function () {
            var pos = $(this).position().left;
            if (pos > 0) {
                $(this).animate({
                    left: initialPosition
                }, 200, null);
            }
        });
    },
    stop: function (event, ui) {
        var size = $(".cell").width();
        if (ui.position.left > initialPosition) {
            if (ui.position.left - initialPosition >= size / 3) {
                ui.helper.animate({
                    left: size - em(4)
                }, 200, null);
            } else {
                ui.helper.animate({
                    left: initialPosition
                }, 200, null);
            }
        }
    }
});

我想在开始拖动之前检测用户是否垂直滚动并取消 水平拖动。

请帮帮我。我怎样才能做到这一点?

【问题讨论】:

  • 我建议使用浮动按钮来上下移动。

标签: javascript html jquery-ui mobile jquery-ui-draggable


【解决方案1】:

我遇到了类似的问题,最终找到了一个相当简单的解决方案。

在我的场景中,我有一个收件箱列表,您可以将其项目拖动到左侧或右侧以显示操作按钮。整个收件箱项目必须是可拖动的——因此不能选择使用拖动手柄。

如果触摸是在draggable 元素内启动的,jQuery 的draggable 会阻止触摸屏上的垂直滚动。因此,如果屏幕上充满了可拖动的收件箱项目,那么用户就会陷入困境——无法向上或向下滚动。

对我有用的解决方案是测量光标垂直位置的任何变化,并使用window.scrollBy 手动滚动窗口相同的量:

var firstY = null;      
var lastY = null;
var currentY = null;
var vertScroll = false;
var initAdjustment = 0;

// record the initial position of the cursor on start of the touch
jqDraggableItem.on("touchstart", function(event) {
    lastY = currentY = firstY = event.originalEvent.touches[0].pageY;
});

// fires whenever the cursor moves
jqDraggableItem.on("touchmove", function(event) {
    currentY = event.originalEvent.touches[0].pageY;
    var adjustment = lastY-currentY;

    // Mimic native vertical scrolling where scrolling only starts after the
    // cursor has moved up or down from its original position by ~30 pixels.
    if (vertScroll == false && Math.abs(currentY-firstY) > 30) {
        vertScroll = true;
        initAdjustment = currentY-firstY;
    }

    // only apply the adjustment if the user has met the threshold for vertical scrolling
    if (vertScroll == true) {
        window.scrollBy(0,adjustment + initAdjustment);
        lastY = currentY + adjustment;
    }

});

// when the user lifts their finger, they will again need to meet the 
// threshold before vertical scrolling starts.
jqDraggableItem.on("touchend", function(event) {
    vertScroll = false;
});

这将非常模仿触摸设备上的原生滚动。

【讨论】:

    【解决方案2】:

    我使用 jquery.ui.touch-punch.js,这对我有用,第 38 行:

    event.preventDefault(); 
    

    Reference

    【讨论】:

    • 请在您的答案中提供代码的主要部分。如果将来该链接断开,您的答案将毫无用处。谢谢
    猜你喜欢
    • 2018-02-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-02-28
    • 2014-12-09
    • 2012-01-02
    • 2016-05-28
    • 2021-11-13
    相关资源
    最近更新 更多