Jquery .trigger('stop') method for .dragga

2019-02-22 02:27发布

问题:

$('#element').draggable ({
    stop: function () {
        alert ('stopped');
        //do some action here
    }
}).trigger('stop');

nothing happens, thought #element is draggable now and event does execute after drag is complete. I tried .triggerHandle as well as 'dragstop' as eventtype, no luck

回答1:

Use this to trigger it instead:

.trigger('dragstop')

If you want it to behave completely as a normal event, use .bind('dragstop', function) to attach it as well, the start option behaves slightly differently.



回答2:

I know this is an old question, but now it is possible to trigger the actual drag event of jquery ui instead of the dragstart and dragstop by using the jQuery simulate plugin.

Here is the code I used because I needed access to the snapping elements of my resizable object (data only accesible on drag stop)

$(this).resizable({
    handles: 'e',
    stop: function (e, ui) {
        var resizable = ui.element;             
        resizable.simulate("mousedown", {clientX: e.clientX, clientY: e.clientY});
        resizable.simulate("mousemove", {clientX: e.clientX + 10, clientY: e.clientY + 10});
        resizable.simulate("mouseup", {clientX: e.clientX, clientY: e.clientY});
    }
});