2015-11-19 367 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,則滾動條可見。