Detect scrollbar dynamically

≡放荡痞女 提交于 2019-12-12 02:29:46

问题


What is wrong with the following code?

I need to detect and set width of 1 element based on if another element has scrollbar or not. this seems not working:

console.log("print Scroll..here...");
if(($(".printer-details-container").offsetHeight < $(".printer-details-container").scrollHeight) || ($(".printer-details-container").offsetWidth < $(".printer-details-container").scrollWidth)){
      // your element have overflow
      console.log("print Scroll..here...");
      $(".printer-header").css({"width":"96.9999%;"});
 }
 else{
      //your element don't have overflow
      console.log("no print Scroll..here...");
      $(".printer-header").css({"width":"100%;"});
      //console.log($(".printer-header").print-header.css({"width":"100%;"});
 }

回答1:


Have a look at this:

https://jsfiddle.net/4Lms5uzz/

At first the second div is set to 50 by 50.

If scroll is present in the #rules div, the dimensions of the second will be changed.

Basically the method is divWithScrollbar.clientHeight < divWithScrollbar.scrollHeight

scrollHeight gets the height of the content inside the div and clientHeight get the size of the div.

if the scrollHeight is smaller than scrollHeight, the scrollbar is visible.



来源:https://stackoverflow.com/questions/33805052/detect-scrollbar-dynamically

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!