我有一个可拖动的元素(使用jquery-ui)。如何基于一些约束条件取消当前的拖拽操作?换句话说,我想限制这个可拖动元素的可能移动,但它比仅基于轴或父级(包含)更加复杂。
我该怎么做呢?
$(*selector*).draggable({
drag: function(event) {
if (*condition*) {
event.preventDefault();
}
}
});
似乎无法取消拖放。到目前为止,我唯一的解决方案是重新绘制所有可拖动的内容...
这是为了将确认对话框应用于拖放操作,大致如下:
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')
}
}
欢迎改进,不确定将删除的元素传递给对话框的方式是否最佳,但对我来说有效。