2013-05-07 81 views
2

我試圖檢測瀏覽器窗口中是否顯示了滾動條。 回答像this這樣的問題可以檢測內容是否可以滾動,但是給出現代的操作系統和瀏覽器,這並不總是意味着實際上和不斷是滾動條。 有誰知道如果/如何可能?如何檢測瀏覽器窗口中是否實際存在滾動條

+0

你能僅僅是內容的大小與窗口的大小? '如果(div高度/寬度> window.height/width'? – 2013-05-07 23:20:56

+0

如果您擔心OSX滾動條僅在實際滾動時可見,請勿覆蓋它們,因此它們不會覆蓋 – bfavaretto 2013-05-07 23:23:36

+0

^與unity/ubuntu中的疊加滾動條相同 – 2013-05-07 23:34:40

回答

0

或者:

if (document.height > $(window).height()) { 
    alert('VERTICAL SCROLLBARS'); 
} 

編輯:使用jQuery從

+0

...除了'document.height'和'window.he ight'確實存在;) – 2013-05-07 23:27:20

+0

你是對的,但它仍然在瀏覽器中被支持。 也許作爲替代document.body.scrollHeight? http://developer.mozilla.org/en-US/docs/dom/element.scrollheight – Reko 2013-05-07 23:30:25

+0

你可能是指'document.body.clientHeight> window.innerHeight',但它不準確。一個很好的測試網站是http://example.iana.org/ - 在控制檯中試用它,使控制檯更高更短,使滾動條出現/消失(編輯:'document.body.scrollHeight'可能是關鍵,是啊) – 2013-05-07 23:31:27

0
function getScrollBarState() { 
    var result = {vScrollbar: true, hScrollbar: true}; 
    try { 
     var root = document.compatMode=='BackCompat'? document.body : document.documentElement; 
     result.vScrollbar = root.scrollHeight > root.clientHeight; 
     result.hScrollbar = root.scrollWidth > root.clientWidth; 
    } catch(e) {} 
    return(result); 
} 

響應:How do I detect if there are scrollbars on a browser window?

相關問題