如何检测浏览器窗口上是否有滚动条?
我需要能够检测浏览器窗口上是否有滚动条(垂直和水平)。我一直在使用这段代码,但它在 Firefox 5 中不能可靠地工作。
JFL.GetScrollbarState = function () {
var myWidth = 0;
var myHeight = 0;
if (document.documentElement && document.documentElement.clientWidth) {
myWidth = document.documentElement.clientWidth;
myHeight = document.documentElement.clientHeight;
} else {
myWidth = document.body.clientWidth;
myHeight = document.body.clientHeight;
}
return ({
vScrollbar: document.body.scrollHeight > myHeight,
hScrollbar: document.body.scrollWidth > myWidth
});
}
是否有更好的方法可以跨浏览器工作。我的浏览器目标是 Firefox 4-5、Chrome、Safari 4+、Opera 10+。
如果您对为什么我需要知道是否有滚动条感兴趣,那是因为我有一些旋转的 CSS3 过渡(由于其旋转的性质)可能会暂时超出当前文档大小的边缘(从而使文档暂时变大)。如果最初没有滚动条,CSS3 过渡可能会导致滚动条在过渡期间显示,然后在过渡完成后消失,从而导致丑陋的滚动条闪烁。如果我知道不存在滚动条,我可以临时添加一个类,将溢出 x 或溢出 y 设置为隐藏,从而防止滚动条在 CSS3 转换期间闪烁。如果滚动条已经存在,我不需要做任何事情,因为它们可能会移动一点,但它们不会在转换过程中打开/关闭。
如果人们不仅能真正判断出滚动条通常是否需要,还能判断出它们是否真的存在,那就加分了。
I need to be able to detect whether there are scrollbars (both vertical and horizontal) on a browser window. I've been using this code but it isn't working reliably in Firefox 5.
JFL.GetScrollbarState = function () {
var myWidth = 0;
var myHeight = 0;
if (document.documentElement && document.documentElement.clientWidth) {
myWidth = document.documentElement.clientWidth;
myHeight = document.documentElement.clientHeight;
} else {
myWidth = document.body.clientWidth;
myHeight = document.body.clientHeight;
}
return ({
vScrollbar: document.body.scrollHeight > myHeight,
hScrollbar: document.body.scrollWidth > myWidth
});
}
Is there a better way to do this that will work cross browser. My browser targets are Firefox 4-5, Chrome, Safari 4+, Opera 10+.
If you're interested in why I need to know if there are scrollbars, it's because I have some spinning CSS3 transitions that (due to the nature of their spinning) may temporarily go beyond the edges of the current document size (thus making the document temporarily larger). If were no scrollbars initially present, the CSS3 transition may cause scrollbars to show up during the transition and then go away when the transition is finished, leading to an ugly scrollbar flash. If I know that there are no scrollbars present, I can temporarily add a class that will set overflow-x or overflow-y to hidden and thus prevent the scrollbar flash during the CSS3 transition. If scrollbars are already present, I don't have to do anything because they may move a little, but they won't go on/off during the transition.
Bonus points if one can actually tell not only if scrollbars would generally be required, but whether they are actually there or not.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
在某些浏览器(Safari 和 IE)中遇到 David 提出的滚动版本的闪烁问题后,我已经确定了这段没有闪烁问题的代码:
它是我正在使用的代码的衍生版本,并且引用了通用技术在粉丝最喜欢的帖子中。它似乎在我尝试过的每个浏览器中都可以工作,甚至在 IE6 中也是如此。出于我的目的,我希望任何失败都返回有滚动条,因此我以这种方式编码了失败条件。
注意:此代码不会检测滚动条是否已使用 CSS 强制打开或关闭。此代码检测是否需要自动滚动条。如果您的页面可能具有控制滚动条的 CSS 设置,那么您可以获取 CSS 并首先进行检查。
After running into flicker problems with the scrolling version proposed by David in some browsers (Safari and IE), I've settled on this code that does not have the flicker problem:
It's a derivative of what I was using and the general technique was referenced in the post that fanfavorite posted. It seems to work in every browser I've tried even in IE6. For my purposes, I wanted any failure to return that there was a scrollbar so I've coded the failure condition that way.
Note: this code does not detect if a scrollbar has been forced on or off with CSS. This code detects if an auto-scrollbar is called for or not. If your page might have CSS settings that control the scrollbar, then you can get the CSS and check that first.
您看过其他帖子吗? 如何检测滚动条的存在(在 HTML iFrame 中使用 Javascript )?
Have you taken a look at this other post? How can I detect a Scrollbar presence ( using Javascript ) in HTML iFrame?
这实际上很容易。这适用于所有现代浏览器:
It's actually pretty easy. This will work in every modern browser: