javascript - 动态检测滚动条

标签 javascript jquery html css scrollbar

下面的代码有什么问题?

我需要根据另一个元素是否有滚动条来检测和设置 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 x 50。

如果 #rules div 中存在滚动条,则第二个 div 的尺寸将发生变化。

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

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

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

关于javascript - 动态检测滚动条,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33805052/

相关文章:

javascript - 如何删除字符串中特定值之前的所有文本

Javascript 函数在模糊或 onclick 上不起作用

javascript - 为 my nativescript-vue 应用程序自定义 ActionBar

javascript - 多次设置背景颜色动画。

javascript - 使用 Native javascript 从 JSOn 中提取字段

javascript - 带有谷歌地图API v3的overlayMouseTarget

jQuery 移动 + 主干 : cannot call methods on listview prior to initialization

html - 在不知道 CSS 高度的情况下垂直对齐?

javascript - 包含 'shaking' 效果的 HTML Canvas 动画

javascript - AngularJS ng-repeat 不循环数组