Detect if a page has a vertical scrollbar?

后端 未结 13 1774
旧时难觅i
旧时难觅i 2020-11-28 02:17

I just want some simple JQ/JS to check if the current page/window (not a particular element) has a vertical scrollbar.

Googling gives me stuff that seems overly comp

相关标签:
13条回答
  • 2020-11-28 02:55

    I found vanila solution

    var hasScrollbar = function() {
      // The Modern solution
      if (typeof window.innerWidth === 'number')
        return window.innerWidth > document.documentElement.clientWidth
    
      // rootElem for quirksmode
      var rootElem = document.documentElement || document.body
    
      // Check overflow style property on body for fauxscrollbars
      var overflowStyle
    
      if (typeof rootElem.currentStyle !== 'undefined')
        overflowStyle = rootElem.currentStyle.overflow
    
      overflowStyle = overflowStyle || window.getComputedStyle(rootElem, '').overflow
    
        // Also need to check the Y axis overflow
      var overflowYStyle
    
      if (typeof rootElem.currentStyle !== 'undefined')
        overflowYStyle = rootElem.currentStyle.overflowY
    
      overflowYStyle = overflowYStyle || window.getComputedStyle(rootElem, '').overflowY
    
      var contentOverflows = rootElem.scrollHeight > rootElem.clientHeight
      var overflowShown    = /^(visible|auto)$/.test(overflowStyle) || /^(visible|auto)$/.test(overflowYStyle)
      var alwaysShowScroll = overflowStyle === 'scroll' || overflowYStyle === 'scroll'
    
      return (contentOverflows && overflowShown) || (alwaysShowScroll)
    }

    0 讨论(0)
  • 2020-11-28 02:56

    try this:

    var hasVScroll = document.body.scrollHeight > document.body.clientHeight;
    

    This will only tell you if the vertical scrollHeight is bigger than the height of the viewable content, however. The hasVScroll variable will contain true or false.

    If you need to do a more thorough check, add the following to the code above:

    // Get the computed style of the body element
    var cStyle = document.body.currentStyle||window.getComputedStyle(document.body, "");
    
    // Check the overflow and overflowY properties for "auto" and "visible" values
    hasVScroll = cStyle.overflow == "visible" 
                 || cStyle.overflowY == "visible"
                 || (hasVScroll && cStyle.overflow == "auto")
                 || (hasVScroll && cStyle.overflowY == "auto");
    
    0 讨论(0)
  • 2020-11-28 02:58

    I tried the previous answer and doesn't seem to be working the $("body").height() does not necessarily represent the whole html height.

    I have corrected the solution as follows:

    // Check if body height is higher than window height :) 
    if ($(document).height() > $(window).height()) { 
        alert("Vertical Scrollbar! D:"); 
    } 
    
    // Check if body width is higher than window width :) 
    if ($(document).width() > $(window).width()) { 
        alert("Horizontal Scrollbar! D:<"); 
    } 
    
    0 讨论(0)
  • 2020-11-28 02:58

    Other solutions didn't work in one of my projects and I've ending up checking overflow css property

    function haveScrollbar() {
        var style = window.getComputedStyle(document.body);
        return style["overflow-y"] != "hidden";
    }
    

    but it will only work if scrollbar appear disappear by changing the prop it will not work if the content is equal or smaller than the window.

    0 讨论(0)
  • 2020-11-28 03:04
    var hasScrollbar = window.innerWidth > document.documentElement.clientWidth;
    
    0 讨论(0)
  • 2020-11-28 03:05

    I use

    function windowHasScroll()
    {
        return document.body.clientHeight > document.documentElement.clientHeight;
    }
    
    0 讨论(0)
提交回复
热议问题