Alternatives to trigger('mouseup') to stop dragging programmatically

可紊 提交于 2020-02-06 18:59:30

问题


As it seems like I have found a bug in jquery:
jquery draggable throws error when 'mouseup' is triggered Can I get some advice on how to implement the following functionality without using the trigger? I want to be able to stop an element to be dragged when a condition has been reached, I have been trying lot's of possibilities but none works automatically, even though the event is fired when condition is true it only takes effect when you "mouseup" the dragger. I'm really trapped on this, any help will be very appreciated. P.D: I have already posted the bug in jquery.ui


回答1:


The following piece of code worked for me well. There isn't any method that cancels only one "dragging", but you can set the position of the object using ui.position

$('#draggablediv').draggable({
        drag: function (event, ui) {
            if (your_condition) { // for example: ui.position.left < 100
                ui.position.left = ui.position.left_old
                ui.position.top = ui.position.top_old
            }
            ui.position.left_old = ui.position.left
            ui.position.top_old = ui.position.top
        }
    })


来源:https://stackoverflow.com/questions/2927020/alternatives-to-triggermouseup-to-stop-dragging-programmatically

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