I have a draggable (jquery-ui) element. How can I cancel current draggig based on some constraints? In other words, I want to constraint possible moves of t开发者_开发知识库his draggable element, but it is more complicated then based only on axis or parent (containment).
How can I do that?
Try using the drag event handler with event.preventDefault();
For example:
$(*selector*).draggable({
drag: function(event) {
if (*condition*) {
event.preventDefault();
}
}
});
You can't cancel the drop it seems. Only solution I have so far is to redraw the all the draggables...
This is for applying a confirmation dialog to the drop and goes something like:
fruitbowl.draw() // my routine which puts draggable elements into the DOM
$("trash").droppable({
drop: function(ev, ui){
$("#areyousure-dialog")
.data('fruit',ui.draggable) // passes the dragged el
.dialog("open")
}
}
$("#areyousure-dialog").dialog({
buttons: {
'Yes I\'m sure': function(){
var fruit = $(this).data('fruit')
fruit.remove()
fruitbowl.draw()
$(this).removeData('fruit').dialog('close')
},
Cancel: function(){
fruitbowl.draw()
$(this).removeData('fruit').dialog('close')
}
}
Open to improvements, not sure if that's the best way to pass the dropped element to the dialog, but works for me.
精彩评论