Detect if a page has a vertical scrollbar?

后端 未结 13 1776
旧时难觅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 03:08

    Let's bring this question back from the dead ;) There is a reason Google doesn't give you a simple solution. Special cases and browser quirks affect the calculation, and it is not as trivial as it seems to be.

    Unfortunately, there are problems with the solutions outlined here so far. I don't mean to disparage them at all - they are great starting points and touch on all the key properties needed for a more robust approach. But I wouldn't recommend copying and pasting the code from any of the other answers because

    • they don't capture the effect of positioned content in a way that is reliable cross-browser. The answers which are based on body size miss this entirely (the body is not the offset parent of such content unless it is positioned itself). And those answers checking $( document ).width() and .height() fall prey to jQuery's buggy detection of document size.
    • Relying on window.innerWidth, if the browser supports it, makes your code fail to detect scroll bars in mobile browsers, where the width of the scroll bar is generally 0. They are just shown temporarily as an overlay and don't take up space in the document. Zooming on mobile also becomes a problem that way (long story).
    • The detection can be thrown off when people explicitly set the overflow of both the html and body element to non-default values (what happens then is a little involved - see this description).
    • In most answers, body padding, borders or margins are not detected and distort the results.

    I have spent more time than I would have imagined on a finding a solution that "just works" (cough). The algorithm I have come up with is now part of a plugin, jQuery.isInView, which exposes a .hasScrollbar method. Have a look at the source if you wish.

    In a scenario where you are in full control of the page and don't have to deal with unknown CSS, using a plugin may be overkill - after all, you know which edge cases apply, and which don't. However, if you need reliable results in an unknown environment, then I don't think the solutions outlined here will be enough. You are better off using a well-tested plugin - mine or anybody elses.

    0 讨论(0)
  • 2020-11-28 03:10

    Oddly none of these solutions tell you if a page has a vertical scrollbar.

    window.innerWidth - document.body.clientWidth will give you the width of the scrollbar. This should work in anything IE9+ (not tested in the lesser browsers). (Or to strictly answer the question, !!(window.innerWidth - document.body.clientWidth)

    Why? Let's say you have a page where the content is taller than the window height and the user can scroll up/down. If you're using Chrome on a Mac with no mouse plugged in, the user will not see a scrollbar. Plug a mouse in and a scrollbar will appear. (Note this behaviour can be overridden, but that's the default AFAIK).

    0 讨论(0)
  • 2020-11-28 03:13

    This one did works for me:

    function hasVerticalScroll(node){
        if(node == undefined){
            if(window.innerHeight){
                return document.body.offsetHeight> window.innerHeight;
            }
            else {
                return  document.documentElement.scrollHeight > 
                    document.documentElement.offsetHeight ||
                    document.body.scrollHeight>document.body.offsetHeight;
            }
        }
        else {
            return node.scrollHeight> node.offsetHeight;
        }
    }
    

    For the body, just use hasVerticalScroll().

    0 讨论(0)
  • 2020-11-28 03:13

    Simply compare the width of the documents root element (i.e. html element) against the inner portion of the window:

    if ((window.innerWidth - document.documentElement.clientWidth) >0) console.log('V-scrollbar active')
    

    If you also need to know the scrollbar width:

    vScrollbarWidth = window.innerWidth - document.documentElement.clientWidth;
    
    0 讨论(0)
  • 2020-11-28 03:14

    I wrote an updated version of Kees C. Bakker's answer:

    const hasVerticalScroll = (node) => {
      if (!node) {
        if (window.innerHeight) {
          return document.body.offsetHeight > window.innerHeight
        }
        return (document.documentElement.scrollHeight > document.documentElement.offsetHeight)
          || (document.body.scrollHeight > document.body.offsetHeight)
      }
      return node.scrollHeight > node.offsetHeight
    }
    
    if (hasVerticalScroll(document.querySelector('body'))) {
      this.props.handleDisableDownScrollerButton()
    }
    

    The function returns true or false depending whether the page has a vertical scrollbar or not.

    For example:

    const hasVScroll = hasVerticalScroll(document.querySelector('body'))
    
    if (hasVScroll) {
      console.log('HAS SCROLL', hasVScroll)
    }
    
    0 讨论(0)
  • 2020-11-28 03:18
        <script>
        var scrollHeight = document.body.scrollHeight;
        var clientHeight = document.documentElement.clientHeight;
        var hasVerticalScrollbar = scrollHeight > clientHeight;
    
        alert(scrollHeight + " and " + clientHeight); //for checking / debugging.
        alert("hasVerticalScrollbar is " + hasVerticalScrollbar + "."); //for checking / debugging.
        </script>
    

    This one will tell you if you have a scrollbar or not. I've included some information that may help with debugging, which will display as a JavaScript alert.

    Put this in a script tag, after the closing body tag.

    0 讨论(0)
提交回复
热议问题