Detect if a page has a vertical scrollbar?

后端 未结 13 1798
旧时难觅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: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().

提交回复
热议问题