Detect if a page has a vertical scrollbar?

后端 未结 13 1829
旧时难觅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

    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;
    

提交回复
热议问题