【问题标题】:jQuery UI Draggable revert to original position if moved less than a certain distance如果移动小于一定距离,jQuery UI Draggable 将恢复到原始位置
【发布时间】:2013-09-03 00:03:54
【问题描述】:

我正在使用 jQuery UI 使对象可拖动。

我知道如何在所有情况下使用以下方法使对象恢复到其原始位置: $("#draggable").draggable({ revert: true });

但是,我怎样才能让对象仅在其向左或向右移动 80% 宽度时才恢复?

【问题讨论】:

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


【解决方案1】:

您可以根据在start 事件中确定的开始位置有条件地还原。

$('.dataCard').draggable({ axis: 'x', 
    revert: function() {
        //determine the start/end positions
        var end = $(this).position().left;
        var start = $(this).data('start');
        //subtract end and start to get the (absolute) distance
        var distance = Math.abs(end - start);
        var width = $(this).width();
        //if the distance is more than 80% of the width don't revert
        if(distance > (width * .8))
        {
             return false;
        }
        //else revert
        return true;
    },
    start: function(){
        //get the start position
        var start = $(this).position().left;
        //store the start position on this element
        $(this).data('start', start);
    }
});

Demonstration

【讨论】:

    猜你喜欢
    • 2012-06-05
    • 1970-01-01
    • 1970-01-01
    • 2013-02-18
    • 2014-07-29
    • 1970-01-01
    • 2011-12-27
    • 2013-10-02
    • 1970-01-01
    相关资源
    最近更新 更多