gpt4 book ai didi

javascript检测滚动条不工作

转载 作者:行者123 更新时间:2023-11-30 05:48:17 24 4
gpt4 key购买 nike

我知道这个问题已经被问得令人作呕,但我很困惑,因为在尝试了几种不同的解决方案后,我仍然无法让我的脚本正确检测滚动条。

这是我目前用来尝试检测滚动条的函数:

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

return false;
}

问题是,即使在我的页面上弹出了一个垂直滚动条,因为页面内容比我笔记本电脑上的窗口长,这个函数仍然说窗口高度等于主体高度。

我还是 javascript/jQuery 的新手,所以如果我遗漏了一个重大错误,请多多包涵。

最佳答案

您需要考虑页面的偏移。

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

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

return false;
}

示例:http://jsfiddle.net/a2rnd/

关于javascript检测滚动条不工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16466959/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com