Reposition element prior to dragging with jQuery UI

╄→гoц情女王★ 提交于 2019-12-05 13:02:06

Live Demo

You can implement a custom grid function much more easily than I would have guessed. This should simplify things for you, since you don't need to worry about repositioning and then using jQuery UI's grid.

// Custom grid
$('#box-3').draggable({
    drag: function( event, ui ) {
        var snapTolerance = $(this).draggable('option', 'snapTolerance');
        var topRemainder = ui.position.top % 20;
        var leftRemainder = ui.position.left % 20;

        if (topRemainder <= snapTolerance) {
            ui.position.top = ui.position.top - topRemainder;
        }

        if (leftRemainder <= snapTolerance) {
            ui.position.left = ui.position.left - leftRemainder;
        }
    }  
});

Also, according to Scott Gonzalez, the grid option is going away in the future since it's the kind of thing that's trivial to implement on its own, so this sets you up better for the future, too.

Instead of just passing type of event as parameter of trigger method, pass the event:

DEMO

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