jQuery resize on both sides

余生长醉 提交于 2019-12-06 05:56:16

问题


I'll try to make it clear with words : I would like to know if there is a way with jquery-ui-resizable to resize an object on the 4 sides at the same time, so that the center of the object stay at the same position.

Here is the sandbox http://jsfiddle.net/V79Ge/

So, it's quite like the aspectRatio=true, but while I resize the object from one side, it would control the 3 other sides

Thank you for your clues!


回答1:


Is this the effect you're after: jsFiddle example.

jQuery:

$('#resizeMe').resizable({
    aspectRatio: true,
    resize: function(event, ui) {
        $(this).css({
            'top': parseInt(ui.position.top, 10) + ((ui.originalSize.height - ui.size.height)) / 2,
            'left': parseInt(ui.position.left, 10) + ((ui.originalSize.width - ui.size.width)) / 2
        });
    }
});

Edit: updated code and jsFiddle to use ui.position and ui.originalSize.




回答2:


Played around with your sandbox a bit and got this.

http://jsfiddle.net/V79Ge/17/

$('#resizeMe').resizable({
    aspectRatio: true,
    resize: function(event, ui) {
        $(this).offset({ top: (ui.originalSize.height-ui.size.height)/2,left:(ui.originalSize.width-ui.size.width)/2});
    }
});



回答3:


May the handles be what you need?

http://jsfiddle.net/V79Ge/1/

Then you'll only have to add the visual feedback to the sides of the box.




回答4:


I made some updates j08691's example. Now it also works with north-west handle.

http://jsfiddle.net/j08691/V79Ge/19/

$('#resizeMe').resizable({
aspectRatio: true,
handles: 'all',
resize: function(event, ui) {
    var posTop = parseInt(ui.position.top, 10);
    var posLeft = parseInt(ui.position.left, 10);
    var oPosTop = parseInt(ui.originalPosition.top, 10);
    var pPosLeft = parseInt(ui.originalPosition.left, 10);
    if(posTop == oPosTop && posLeft == pPosLeft)
    {
        $(this).css({
            'top': posTop + ((ui.originalSize.height - ui.size.height)) / 2,
            'left': posLeft + ((ui.originalSize.width - ui.size.width)) / 2
        });
    }
    else {
        var width = ui.size.width;
        var height = ui.size.height;
        var oWidth = ui.originalSize.width;
        var oHeight = ui.originalSize.height;
        $(this).css({                   
            'width': width + (width - oWidth),
            'height': height + (height - oHeight)
        });                
    }
}});

It still need more development. Not working with all handles.




回答5:


      $(".nodecontainer").each(function () {
            $(this).parent().attr('style', $(this).attr('style'));
            $(this).spectrum({
                showPalette: true,
                change: function (color) {
                    $(this).parent().css('background-color', color.toHexString());
                },
                palette: [
                    ['black', 'white', 'blanchedalmond'],
                    ['rgb(255, 128, 0);', 'hsv 100 70 50', 'lightyellow']
                ]
            })
        });;
        $('.nodecontainer').each(function () {
            $(this).resizable({
                alsoResize: $(this).parent(),
                resize: function (e, ui) {
                    $(this).css("top", 0);
                    $(this).css("left",0);
                    $(this).css("position","relative");
                }
            });
        });


来源:https://stackoverflow.com/questions/9892585/jquery-resize-on-both-sides

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