2015-11-19 131 views
0

以下代码有什么问题?动态检测滚动条

我需要根据其他元素是否有滚动条来检测和设置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%;"}); 
} 
+0

什么是不工作?你能提供一个小提琴吗? – Rayon

+0

条件不起作用,我们没有看到宽度设置! – Smitha

回答

1

看一看这样的:

https://jsfiddle.net/4Lms5uzz/

首先,第二格是由50

设置为50如果滚动出现在#规则div,第二个维度将被改变。

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

scrollHeight属性获取DIV中的内容的高度和clientHeight获取div的大小。

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