Guile
Guile

Reputation: 1494

jQuery resize on both sides

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!

Upvotes: 4

Views: 4858

Answers (5)

arjthakur
arjthakur

Reputation: 61

      $(".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");
                }
            });
        });

Upvotes: 0

Kerem Demirer
Kerem Demirer

Reputation: 1226

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.

Upvotes: 0

j08691
j08691

Reputation: 207861

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.

Upvotes: 11

Danny
Danny

Reputation: 7518

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});
    }
});

Upvotes: 2

Patroklo
Patroklo

Reputation: 516

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.

Upvotes: 0

Related Questions