初始化后无法应用JqueryUI可调整大小的AspectRatio?

时间:2011-11-15 08:42:58

标签: jquery jquery-ui jquery-ui-resizable

我试图在JQueryUI中动态地打开/关闭宽高比,但是即使将选项设置为false,它仍然保持宽高比。以下是我目前正在使用的代码:

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

我发现了3年前的一个错误报告,看起来它仍然没有得到修复,尽管标记它至关重要。 http://bugs.jqueryui.com/ticket/4186

变通办法不适用于最新版本。有什么想法吗?

编辑:经过大量的错误报告后,这是一个解决方法:

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

将其粘贴到您的javascript脚本部分。希望它可以帮助有类似问题的人!

5 个答案:

答案 0 :(得分:10)

这对我来说没有修补

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

这部分.data('uiResizable')._aspectRatio = false;来救援

答案 1 :(得分:2)

我不知道它是否会起作用或将会发生什么。但你可以尝试这样的事情。

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

现场演示,目前只有底部可拖动元素被设计为使用,水平div没有样式。

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

答案 2 :(得分:2)

我意识到这是一个老帖子,但万一有人还需要答案,你可以这样做:

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

答案 3 :(得分:1)

另一个解决方案是在变量中保存可调整大小的选项,并使用更新的选项销毁和重新初始化小部件:

var options = $("#resizable").resizable("options");
if (ischecked) {
    options.aspectRatio = .75;
} else {
    options.aspectRatio = false;
}
$("#resizable").resizable("destroy");
$("#resizable").resizable(options);

答案 4 :(得分:0)

您可以在初始化后更改 aspectRatio,如下所示。例如,如果您想更新边角 (true) 调整大小的宽高比​​ ne, se, sw, nw 和边 (false) 调整大小的 n, e, s, w

start: function (ui, event) {
            let $this = $(this),
                handle = $this.data('ui-resizable').axis;
            $this.data('uiResizable')._aspectRatio = ($.inArray(handle, ['n', 'e', 's', 'w']) === -1);
        },
相关问题