jQueryUI sortable,draggable revert event

柔情痞子 提交于 2019-12-14 01:16:14

问题


I have a problem with jQuqeryUI. I would like to run some code whenever an element is dragged and dropped on an invalid target. As far as I can see, there is no predefined event for this, but I have thought about combining information from following events: over, out, remove and stop, to achieve this. However that might sound like it's going to be a mess, is there a cleaner way yo do it?


回答1:


You can pass a method to the revert option as well, for example if we take the example demo like this:

<div id="draggable" class="ui-widget-content">
    <p>I revert when I'm not dropped</p>
</div>
<div id="droppable" class="ui-widget-header">
    <p>Drop me here</p>
</div>​

Then on your draggable you can calculate whether to revert, like this:

$("#draggable").draggable({ 
    revert:  function(dropped) {
       var dropped = dropped && dropped[0].id == "droppable";
       if(!dropped) alert("I'm reverting, what a cruel world!");
       return !dropped;
    } 
});

You can give it a try here, the dropped parameter passed in is the element it was dropped on that's a .droppable(), it's just false if it landed anywhere else.

Or if you're using the accept option you may want to calculate off that, like this:

var dropped = dropped && $(this).is(dropped.droppable('option', 'accept'));

This uses the accept selector and uses .is() to see if the droppable matches.

You can view a demo of that here.



来源:https://stackoverflow.com/questions/3487155/jqueryui-sortable-draggable-revert-event

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