如果在调整大小时保持其宽高比,则识别可调整大小的Div

时间:2016-01-05 07:00:48

标签: javascript jquery html css jquery-ui

我在下面有HTML代码:

<div id="myid_templates_editor_text_1" class="myid_templates_editor_element myid_templates_editor_text ui-resizable ui-draggable" style="width: 126.79999999999998px; height: 110px; position: absolute; left: 196px; top: 76px; -webkit-transform: rotate(0deg); border: hidden;">
     <div class="myid_templates_editor_text_dynamic_part"><p><strong>dsdasadsd</strong></p></div><div class="ui-resizable-handle ui-resizable-e" style="z-index: 90;"></div>
     <div class="ui-resizable-handle ui-resizable-s" style="z-index: 90;"></div>
     <div class="ui-resizable-handle ui-resizable-se ui-icon ui-icon-gripsmall-diagonal-se" style="z-index: 90;"></div>
</div>

我使div的ID myid_templates_editor_text_1可以调整大小,并通过Jquery Plugin保持其宽高比。

$('#myid_templates_editor_text_1').resizable({
    animate: true,          
    aspectRatio: true
});

有时,我禁用了“保持纵横比”功能。请参阅以下代码:

$('#myid_templates_editor_text_1').resizable({
    animate: true,          
    aspectRatio: false
});

如何通过查看divHTML Codejavascript功能来确定jquery是否保持宽高比?这可能吗?

1 个答案:

答案 0 :(得分:3)

是的,您可以使用option()方法使用许多jQuery UI小部件正常获取选项值。请参阅resizeable()的jQuery UI文档中的示例:

console.log({}.hasOwnProperty("toString")); // false

所以对你的情况来说:

// Getter
var aspectRatio = $( ".selector" ).resizable( "option", "aspectRatio" );

JFIDDLE: https://jsfiddle.net/xnt15Lza/

<强>样本:

&#13;
&#13;
var aspectRatio = $('#myid_templates_editor_text_1').resizable( "option", "aspectRatio" );
&#13;
$(function() {
  $("div.green").resizable({
    aspectRatio: true
  });
  $("div.blue").resizable({
    aspectRatio: false
  });
  var aspectRatio = $("div.green").resizable("option", "aspectRatio");
  alert('green div aspectRatio set to: ' + aspectRatio);
  var aspectRatio = $("div.blue").resizable("option", "aspectRatio");
  alert('blue div aspectRatio set to: ' + aspectRatio);
});
&#13;
div {
  margin: 5px;
  width: 100px;
  height: 100px;
  color: #FFF;
  text-align: center;
}
div.green {
  background-color: green;
}
div.blue {
  background-color: blue;
}
&#13;
&#13;
&#13;