JqueryUI Draggable - Auto resize parent container

安稳与你 提交于 2019-11-29 15:08:29
Hussein

Here you go. Works both horizontally and vertically. See it in action at http://jsfiddle.net/evunh/1/

var i = $('#inner');
var o = $('#outer');
i.draggable({
    drag: function(event, ui) {
        if (i.position().top > o.height() - i.height()) {
            o.height(o.height() + 10);
        }
        if (i.position().left > o.width() - i.width()) {
            o.width(o.width() + 10);
        }
    }
});

You can use the drag function that comes with jquery draggable. In the drag function you can just check if the edges of the inner div is outside of the container div's edges.

If thay are, increase the width or height of the container div.

You can use the position method that is native in jquery ui to check for positions of top,bottom,left and right of both div tags.

Here are links to the api for more detailed information about how these methods work.

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