Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>If you want to revert the element to the source position if it's not dropped inside a <code>#droppable</code> element, just save the original parent element of the draggable at the start of the script (instead of the position), and if you verify that it's not dropped into <code>#droppable</code>, then just restore the parent of <code>#draggable</code> to this original element.</p> <p>So, replace this:</p> <pre><code>}).each(function() { var top = $(this).position().top; var left = $(this).position().left; $(this).data('orgTop', top); $(this).data('orgLeft', left); }); </code></pre> <p>with this:</p> <pre><code>}).each(function() { $(this).data('originalParent', $(this).parent()) }); </code></pre> <p>Here, you'll have the original parent element of the draggable. Now, you have to restore it's parent in a precise moment.</p> <p><code>drop</code> is called every time the element is dragged out from the droppable, not at the stop. So, you're adding a lot of event callbacks. This is wrong, because you never clean the <code>mouseup</code> event. A good place where you can hook a callback and check if the element was dropped inside or outside the <code>#droppable</code> element, is <code>revert</code>, and you're doing it right now, so, just delete the <code>drop</code> callback.</p> <p>When the element is dropped, and needs to know if it should be reverted or not, you know for sure that you'll not have any other interaction from the user until the new drag start. So, using the same condition you're using to know if it should revert or know, let's replace this <code>alert</code> with a fragment of code that: restores the parent element to the original div, and resets the <code>originalPosition</code> from the <code>draggable</code> internals. The <code>originalPosition</code> proeprty is setted at the time of <code>_mouseStart</code>, so, if you change the owner of the element, you should reset it, in order to make the animation of revert go to the proper place. So, let's set this to <code>{top: 0, left: 0}</code>, making the animation go to the origin point of the element:</p> <pre><code>revert: function(dropped) { var dropped = dropped &amp;&amp; dropped[0].id == "droppable"; if(!dropped) { $(this).data("draggable").originalPosition = {top:0, left:0} $(this).appendTo($(this).data('originalParent')) } return !dropped; } </code></pre> <p>And that's it! You can check this working here: <a href="http://jsfiddle.net/eUs3e/1/" rel="nofollow">http://jsfiddle.net/eUs3e/1/</a></p> <p>Take into consideration that, if in any jQuery's UI update, the behavior of <code>revert</code> or <code>originalPosition</code> changes, you'll need to update your code in order to make it work. Keep in mind that.</p> <p>If you need a solution which doesn't make use of calls to the internals of ui.draggable, you can make your <code>body</code> an droppable element with <code>greedy</code> option defined as <code>false</code>. You'll have to make sure that your <code>body</code> elements take the full screen.</p> <p>Good luck!</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload