简体   繁体   English

防止ui可调整大小的句柄自动触发溢出滚动条

[英]Prevent ui-resizable handles from automatically triggering overflow scrollbars

Hi I use jqueryui/resizable for a <div id="blue"> element that contains a child <div id="red"> with some inner html text content. 嗨,我使用jqueryui / resizable作为<div id="blue">元素,其中包含带有一些内部html文本内容的子<div id="red"> The blue is $(".resizable").resizable(); 蓝色是$(".resizable").resizable(); and I want that scrollbars appear if I resize blue smaller than the content of red. 如果我调整大小小于红色内容的蓝色,我希望滚动条出现。

The problem is that because of the classes .ui-resizable-s .ui-resizable-e the scrollbars are always visible. 问题是因为类.ui-resizable-s .ui-resizable-e滚动条总是可见的。

Just look at this JSFiddle https://jsfiddle.net/662tyqv8/4/ 看看这个JSFiddle https://jsfiddle.net/662tyqv8/4/

If I now hide the horizontal and vertical handles in the JSFiddle, it works ( the overflow scrollbars are only there if needed. 如果我现在隐藏JSFiddle中的水平和垂直手柄,它可以工作(溢出滚动条只在需要时才存在)。

If I look at the DOM Tree, I understand why the scrollbars are always visible: 如果我查看DOM树,我理解为什么滚动条始终可见:

<div id="blue" class="resizable ui-resizable">
    <div id="red">C<br>O<br>N<br>T<br>E<br>X<br>T</div>

    <div class="ui-resizable-handle ui-resizable-e" style="z-index: 1000;"></div>
    <div class="ui-resizable-handle ui-resizable-s" style="z-index: 1000;"></div>
    <div class="ui-resizable-handle ui-resizable-se ui-icon ui-icon-gripsmall-diagonal-se" style="z-index: 1000;"></div>
</div>

This picture shows that the horizontal handle is outside of the area of the blue <div> and therefore it automatically adds the scroll bar 此图显示水平手柄位于蓝色<div>区域之外,因此它会自动添加滚动条

This is done in resize where you compare the height of the two elements: 这是在resize完成的,您可以在其中比较两个元素的高度:

https://jsfiddle.net/Twisty/mf52vhe5/ https://jsfiddle.net/Twisty/mf52vhe5/

HTML HTML

<div id="blue" class=" resizable">
  <div id="red">
    C
    <br>O
    <br>N
    <br>T
    <br>E
    <br>X
    <br>T
  </div>
</div>

CSS CSS

#blue {
  width: 200px;
  height: 200px;
  background: blue;
  outline: 1px solid black;
  position: absolute !important;
  overflow-x: hidden;
  overflow-y: hidden;
}

#red {
  background: red;
}

jQuery jQuery的

 $(function() {
   $(".resizable").resizable({
     resize: function(e, ui) {
       var blue = $("#blue");
       var red = $("#red");
       if (blue.height() < red.height()) {
         blue.css("overflow-y", "auto");
       } else {
         blue.css("overflow-y", "hidden");
       }
     }
   });
 });

You can also do this by add/removing a class or toggling a class. 您也可以通过添加/删除类或切换类来完成此操作。

.scrolling {
  overflow-y: auto;
}

Then do this when comparing: 然后在比较时执行此操作:

if (blue.height() < red.height()) {
    blue.addClass("scrolling");
} else {
    blue.removeClass("scrolling");
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM