Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Unable to apply JqueryUI Resizable AspectRatio after initialization?

I was trying to turn the aspect ratios on/off dynamically in JQueryUI resizable, however even after setting the option to false, it keeps maintaining the aspect ratio. Below is the code I am currently working with:

$('#aspect_check').click( function() {
    var ischecked = $('#aspect_check').prop('checked');
    if (ischecked) {
    $( "#resizable" ).resizable( "option", "aspectRatio", .75);
    } else {
        $( "#resizable" ).resizable( "option", "aspectRatio", false );
    }
});

I found a bug report from 3 years ago, which looks like it still hasn't been fixed despite marking it critical. http://bugs.jqueryui.com/ticket/4186

The workarounds don't work with the latest versions. Any ideas?

Edit: After going through a lot of bug reports, here is a work around:

$(function() {
$.extend($.ui.resizable.prototype, (function (orig) {
    return {
        _mouseStart: function (event) {
            this._aspectRatio = !!(this.options.aspectRatio);
            return(orig.call(this, event));
        }
    };
})($.ui.resizable.prototype["_mouseStart"]));
});

Paste it in your javascript script section. Hope it helps someone with similar problem!

like image 311
Alvin Avatar asked Nov 15 '11 08:11

Alvin


3 Answers

I don't know if it will work or what will happen. But you can try something like this.

$(function(){
    var settings = {
        aspectRatio : .75  
    };
    $("#tadaa").resizable(settings);

    $("input").change(function(){
        if($(this).is(":checked"))
        {

            $("#tadaa").resizable( "destroy" );
            $("#tadaa").resizable();
        }
        else  
        {
            $("#tadaa").resizable( "destroy" );
            $("#tadaa").resizable(settings);
        }   
    });
});

Live demo, currently only the bottom draggable element is styled to use, horizontal div is not styled.

http://jsfiddle.net/t6xFN/1/

like image 112
Niels Avatar answered Nov 19 '22 13:11

Niels


This worked for me with no patching

$('#resizable')
  .resizable('option', 'aspectRatio', false)
  .data('uiResizable')._aspectRatio = false;

This part .data('uiResizable')._aspectRatio = false; comes to the rescue

like image 32
unloco Avatar answered Nov 19 '22 12:11

unloco


I realize that this is an old post but in case anyone still needs an answer you can do it like this:

$('#aspect_check').click( function() {
    var ischecked = $('#aspect_check').prop('checked');
    if (ischecked) {
        $( "#resizable" ).resizable({aspectRatio : .75});
    } else {
        $( "#resizable" ).resizable();
    }
});
like image 2
RobertH Avatar answered Nov 19 '22 13:11

RobertH