动态检测滚动条

Detect scrollbar dynamically

本文关键字:滚动条 检测 动态      更新时间:2023-09-26

以下代码有什么问题?

我需要根据另一个元素是否有滚动条来检测和设置1个元素的宽度。这似乎不起作用:

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%;"});
 }

看看这个:

https://jsfiddle.net/4Lms5uzz/

首先,第二个div设置为50乘50。

如果#rulesdiv中存在滚动,则第二个的维度将发生更改。

基本上方法是divWithScrollbar.clientHeight < divWithScrollbar.scrollHeight

scrollHeight获取div内部内容的高度,clientHeight获取div.的大小

如果scrollHeight小于scrollHeith,则滚动条可见。