繁体   English   中英

如何在JQuery中找出浏览器窗口是否有可见的滚动条?

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

我想知道是否有办法在JQuery中找出浏览器窗口是否有可见的滚动条?

这是我正在使用的代码:

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

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

任何帮助都非常感谢,谢谢

使用以下功能。

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;
}

如果比较(window).height()(document).height()会发生什么情况如果文档高度大于窗口高度那么滚动条应该是可见的但这也取决于您的CSS设置以及是否隐藏溢出或可见。

编辑

您需要创建一个侦听器,以便代码在正确的时间运行。 这应该在调整浏览器窗口大小时起作用:

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

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM