jQuery - 可调整大小的宽高比​​仅适用于单手柄

时间:2013-08-02 11:50:48

标签: javascript jquery jquery-ui resize jquery-ui-resizable

我只想保护一个手柄的纵横比。我尝试使用此代码运行它,但它没有帮助我。你有任何解决方案吗?

  $( "#resizable").resizable({
   handles: 'se,e,w',
   aspectRatio: true,
    start: function(e,ui) {
      if (jQuery(e.originalTarget).hasClass("ui-resizable-se")) {
         aspectRatio: false;
      }
    }
      });

特别感谢任何解决方案..

编辑:问题已解决。您可以在此处查看解决方案:https://stackoverflow.com/a/18101710/2572291

4 个答案:

答案 0 :(得分:2)

关于这个错误有很多未解答的问题。

以下是适合您的解决方案:)

http://jsfiddle.net/882k9/

    <script>
    var changedRatio  = "DK"; //DK->dont keep
    var nowResizing   = "NO";
    $(function() {
      $('#resizable').resizable({
          start:function(event,ui){
            nowResizing = "YES";
          },
          stop:function(event,ui){
            nowResizing = "NO";
          }
        });
      $(document).on('mouseenter', '.ui-resizable-e', function(){
          if(changedRatio!="DK"){
            if(nowResizing!="YES"){
              var targetDiv = $(this).parent().attr("id");
              dontKeep(targetDiv);
            }
          }
      });

      $(document).on('mouseenter', '.ui-resizable-se', function(){
          if(changedRatio!="K"){
            if(nowResizing!="YES"){
              var targetDiv = $(this).parent().attr("id");
              keep(targetDiv);
            }
          }
      });
      $(document).on('mouseenter', '.ui-resizable-s', function(){
          if(changedRatio!="DK"){
            if(nowResizing!="YES"){
              var targetDiv = $(this).parent().attr("id");
              dontKeep(targetDiv);
            }
          }
      });
    });

function dontKeep(val){
    $("#"+val).resizable("destroy");
    $("#"+val).resizable({
      aspectRatio:false,
      start:function(event,ui){
          nowResizing = "YES";
        },
      stop:function(event,ui){
          nowResizing = "NO";
      }
    });
    changedRatio  = "DK";
  }
  function keep(val){
    $("#"+val).resizable("destroy");
    $("#"+val).resizable({
      aspectRatio:true,
      start:function(event,ui){
          nowResizing = "YES";
        },
      stop:function(event,ui){
          nowResizing = "NO";
      }
    });
    changedRatio  = "K";
  }
    </script>


<div id="resizable" class="ui-widget-content">
  <h3 class="ui-widget-header">Resizable</h3>
</div>

答案 1 :(得分:1)

一个对我有用的简单解决方案

var is_se = ($(this).data('uiResizable').axis == "se");
$(this).resizable( "option", "aspectRatio", is_se ).data('uiResizable')._aspectRatio = is_se;

答案 2 :(得分:1)

略微变化对我有用,可能是因为更新版本的jQuery。四个角的把手将保持纵横比,而侧面的把手则不会。

$elements.resizable({
  handles: "all",

  start: function(event, ui) {
    var handle = $(this).data('ui-resizable').axis;

    // We should maintain the aspect ratio for corners, not for sides
    var maintain_aspect_ratio = (["n", "e", "s", "w"].indexOf(handle) == -1);

    $(this).resizable( "option", "aspectRatio", maintain_aspect_ratio )
      .data('ui-resizable')
      ._aspectRatio = maintain_aspect_ratio;
  }, 

  ...

答案 3 :(得分:0)

我认为你应该提供宽高比而不是true

aspectRatio: 16 / 9 

OR

aspectRatio: 4 / 3 

参考http://jqueryui.com/resizable/#aspect-ratio

OR

$( "#resizable:not(ui-resizable-se)").resizable({
   handles: 'se,e,w',
   aspectRatio: 16/9
});