编程语言
首页 > 编程语言> > javascript-如何手动触发jQuery drag()函数?像$(element).trigger(‘drag’);

javascript-如何手动触发jQuery drag()函数?像$(element).trigger(‘drag’);

作者:互联网

我有一个可拖动的img元素.现在,我想手动触发drag()事件,就像我们如何以$(element).trigger(‘click’)触发“ click”事件一样.谢谢.

这是我的功能定义

$('#imgToUpload').draggable({
        drag: function(event, ui) {
            if (ui.position.top > 0) {
                ui.position.top = 0;
            }
            var maxtop = ui.helper.parent().height() - ui.helper.height();
            if ( ui.position.top < maxtop) {
                ui.position.top = maxtop;
            }
            if ( ui.position.left > 0) {
                ui.position.left = 0;
            }
            var maxleft = ui.helper.parent().width() - ui.helper.width();
            if ( ui.position.left < maxleft) {
                ui.position.left = maxleft;
            }
            ImageCropping.calculateCroppedCoordinates();
        }
    });

解决方法:

这是jQuery ui插件

https://github.com/jquery/jquery-ui/blob/9e8e339648901899827a58e5bf919f7dda03b88e/tests/jquery.simulate.js

将其包含在页面上,然后简单地使用

$(“#myElement”).simulate(‘drag’);

标签:jquery-draggable,jquery-ui-draggable,draggable,javascript,jquery
来源: https://codeday.me/bug/20191027/1943687.html