how to revert position of a jquery UI draggable based on condition

前端 未结 5 2014
孤独总比滥情好
孤独总比滥情好 2020-12-08 04:54

I have an element which is draggable and an element which is droppable. Once the dragged item is dropped on the dropzone I am trying to execute the following jquery psuedo c

5条回答
  •  刺人心
    刺人心 (楼主)
    2020-12-08 05:02

    There are some built-in options for this, on your .draggable(), set the revert option to 'invalid', and it'll go back if it wasn't successfully dropped onto a droppable, like this:

    $("#draggable").draggable({ revert: 'invalid' });
    

    Then in your .droppable() set what's valid for a drop using the accept option, for example:

    $("#droppable").droppable({ accept: '#draggable' });​
    

    Anything not matching this selector gets reset when you let go, you can see a full demo here. The accept option also takes a function if you need filtering a selector can't provide, like this:

    $("#droppable").droppable({ 
      accept: function(dropElem) {
        //dropElem was the dropped element, return true or false to accept/refuse it
      }
    });​
    

提交回复
热议问题