JQuery sortable () doesn't work after $ (document) .unbind ('mousemove')

JQuery Sortable () works well, and if I try to destroy and create a sort, it also works well. but if you try $(document).unbind('mousemove') and recreate the sort, it only works once, and then it will never work. I know that I can change the code. but I want to know why.

here is the code below, also on jsfiddle ( http://jsfiddle.net/webjjin/YJEf5/ )

 <link rel="stylesheet" href="http://code.jquery.com/ui/1.10.3/themes/smoothness/jquery-ui.css"> <script src="http://code.jquery.com/jquery-1.9.1.js"></script> <script src="http://code.jquery.com/ui/1.10.3/jquery-ui.js"></script> <div id="container"> <ul id="sortable"> <li>Item 1</li> <li>Item 2</li> <li>Item 3</li> <li>Item 4</li> <li>Item 5</li> </ul> </div> <button id="btn">Destroy and create</button> <button id="unbind">Unbind</button> <script>$("#sortable").sortable();</script> <script> var html = $('#container').html(); $('#btn').click(function(){ $("#sortable").sortable('destroy'); $('#container').empty(); setTimeout(function(){ $('#container').append(html); $("#sortable").sortable(); }, 500); }); $('#unbind').click(function(){ jQuery(document).unbind('mousemove').unbind('mouseup'); }) </script> 
+4
source share
2 answers

Using this code

 jQuery(document).unbind('mousemove').unbind('mouseup'); 

you remove ALL mousemove and mouseup event listeners on the page, which are necessary for jQuery sorting. It is used for functions such as tracking the position in which a moveable item is moved or discarded. Thus, disabling this will disrupt the entire process.

If you want to untie these event lists, use a special selector:

 $('#test').unbind('mousemove').unbind('mouseup'); 
+3
source

Finally, I found the reason.

in jQuery-ui file. There is this code.

 var mouseHandled = false; $( document ).mouseup( function() { mouseHandled = false; }); 

and function _mouseDown, for example,

 if( mouseHandled ) { return; } 

That's why if try jQuery (document) .unbind ('mouseup'), work on sortable widgets does not include other jquery-ui widgets.

Thanks to everyone.

+1
source

Source: https://habr.com/ru/post/1483704/


All Articles