drag event for jquery-ui-sortable

こ雲淡風輕ζ 提交于 2020-01-09 10:03:51

问题


How to listen to drag event when a jquery-ui-sortable is being dragged?

By hit-n-trial strategy, I've tried drag event from jquery-ui-draggable but it's not working.

$('.widget_container').sortable({
    drag: function(event, ui) { console.log('drag'); }
});

回答1:


Use sort event for this purpose:

$(".sortable").sortable({
    sort: function(e) {
      console.log('X:' + e.screenX, 'Y:' + e.screenY);
    }
});

http://jsbin.com/kegeb




回答2:


If you need to handle the event when the user starts to drag, you should use handle instead of sort :

$(".sortable").sortable({
    handle: function(e) {
      console.log('Handled');
    }
});

This event will occurs at the beginning of the drag, and when the page is loaded (http://api.jqueryui.com/sortable/#option-handle).

I suggest you to also check this answer, which explains the correct ways to handle your elements when your list is updated : Get order of list items in a jQuery Sortable list after resort

Good luck




回答3:


On the documentation it says you shall use "sort" instead of "drag".

http://api.jqueryui.com/sortable/#option-forcePlaceholderSize



来源:https://stackoverflow.com/questions/8148145/drag-event-for-jquery-ui-sortable

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!