Detect if a page has a vertical scrollbar?

后端 未结 13 1800
旧时难觅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: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:<"); 
    } 
    

提交回复
热议问题