问题
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