简体   繁体   中英

How to stop the resize for a draggable element Jquery Ui?

I have a working area ,where I can drag&drop and resize my element.

$(objName).draggable({
    start: function (ev, ui) {$(this).css({opacity:0.3})},

    cursor:'move',
    containment: 'parent',
    snap:true,
    stop: function (ev, ui) {
        $(this).css({opacity:1});
        var pos = $(ui.helper).offset();
        console.log($(this).attr("id"));
        console.log(pos.left)
        console.log(pos.top)
    }
})
.resizable({ghost:true,

});

But if I start resizing it ,the size can't be fixed by the borders of working area.

What you need to do is confine the Element on the resize as well as the Draggable to the working area like so

$(objName).draggable({
    start: function (ev, ui) {$(this).css({opacity:0.3})},

    cursor:'move',
    snap:true,
    drag: function(e, ui){
        var topRightCornerPos_x = $(this).css('left') + $(this).width();
        var bottomLeftConerPos_y = $(this).css('top') + $(this).width();

        if(topRightCornerPos_x > $(this).parent().width()){
            var maxLeft = $(this).parent().width() - $(this).width();
            $(this).css('left', maxLeft);
        }
        if(bottomLeftConerPos_y  > $(this).parent().height()){
            var maxTop = $(this).parent().height() - $9this).height();
            $(this).css('top', maxTop);
        }
    },
    stop: function (ev, ui) {
        $(this).css({opacity:1});
        var pos = $(ui.helper).offset();
        console.log($(this).attr("id"));
        console.log(pos.left)
        console.log(pos.top)
    }
})
.resizable({ghost:true,
    containment: $('#workingarea_id')
});

or again you can use containment: 'parent' in the resizable params

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM