Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I find out whether the browser window has a scrollbar visible, in JQuery?

Tags:

jquery

I was wondering if there is a way of finding out whether a browser window has a scrollbar visible, in JQuery?

Here's the code I'm working with:

var hContent = $("body").height(); 
var hWindow = $(window).height(); 

if(hContent>hWindow) {
    $('#scroll-top').fadeIn(250);    
}
else {
    $('#scroll-top').fadeOut(250);
}

Any help is Greatly Appreciated, Thanks

like image 559
Nasir Avatar asked Dec 13 '22 16:12

Nasir


1 Answers

Use the following function.

function checkScrollBar() {
    var hContent = $("body").height(); // get the height of your content
    var hWindow = $(window).height();  // get the height of the visitor's browser window

    // if the height of your content is bigger than the height of the 
    // browser window, we have a scroll bar
    if(hContent>hWindow) { 
        return true;    
    }

    return false;
}
like image 60
niksvp Avatar answered May 02 '23 10:05

niksvp