Jquery:可拖动的敏感性

Rai*_*ain 3 javascript jquery jquery-ui jquery-ui-draggable

我有一个使用 jquery 的可拖动 DOM 元素。

如果客户端单击它而不移动,则不会调用可拖动的开始,并且它的行为就像不是可拖动的一样。

当客户端单击它并按住鼠标将鼠标移开 1 个像素时,拖动开始。事件被触发,类被应用等等......

只有当客户端单击元素并将鼠标移动至少 10px 时,才可以触发拖动开始吗?

注意:这与滚动无关。

来自https://jqueryui.com/draggable/现场演示的代码示例:

<style>#draggable { width: 150px; height: 150px; padding: 0.5em; }</style>
<script src="https://code.jquery.com/jquery-1.12.4.js"></script>
<script src="https://code.jquery.com/ui/1.12.0/jquery-ui.js"></script>

<script>
$(function(){
    $("#draggable").draggable(); 
    //goal is to only starts dragging if mouse moved more than 10px
});
</script>

<div id="draggable" class="ui-widget-content">
    <p>Drag me around</p>
</div>
Run Code Online (Sandbox Code Playgroud)

Jul*_*les 5

看起来您想使用“距离”属性。http://api.jqueryui.com/draggable/#option-distance

“鼠标按下后鼠标必须移动的像素距离,然后才能开始拖动。此选项可用于在单击元素时防止不必要的拖动。”

$( "#draggable" ).draggable({
  distance: 10
});
Run Code Online (Sandbox Code Playgroud)