我只想要一些简单的JQ/JS代码来检查当前页面/窗口(而不是特定元素)是否有垂直滚动条。
谷歌搜索给出的结果似乎对于这个基本功能来说过于复杂了。
如何实现呢?
我只想要一些简单的JQ/JS代码来检查当前页面/窗口(而不是特定元素)是否有垂直滚动条。
谷歌搜索给出的结果似乎对于这个基本功能来说过于复杂了。
如何实现呢?
$(document).ready(function() {
// Check if body height is higher than window height :)
if ($("body").height() > $(window).height()) {
alert("Vertical Scrollbar! D:");
}
// Check if body width is higher than window width :)
if ($("body").width() > $(window).width()) {
alert("Horizontal Scrollbar! D:<");
}
});
试一下这个:
var hasVScroll = document.body.scrollHeight > document.body.clientHeight;
这只会告诉你垂直滚动高度是否大于可视内容的高度。变量hasVScroll
将包含true或false。
如果您需要进行更彻底的检查,请将以下代码添加到上面的代码中:
// Get the computed style of the body element
var cStyle = document.body.currentStyle||window.getComputedStyle(document.body, "");
// Check the overflow and overflowY properties for "auto" and "visible" values
hasVScroll = cStyle.overflow == "visible"
|| cStyle.overflowY == "visible"
|| (hasVScroll && cStyle.overflow == "auto")
|| (hasVScroll && cStyle.overflowY == "auto");
cStyle.overflow === 'scroll'
? - B Tdocument.body
时,cStyle.overflow == "visible"
可以显示滚动条。但应该是 (hasVScroll && cStyle.overflow == "visible" && element.nodeName == "BODY")。此外,还需要检查 cStyle.overflow === 'scroll'
,正如您所指出的那样。 - mseifert我尝试了之前的回答,但似乎不起作用。$("body").height()并不一定表示整个HTML的高度。
我已经纠正了解决方案,如下:
// 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:<");
}
$( document ).width()
和 .height()
的答案则会受到 jQuery 对文档大小检测的错误 的影响。window.innerWidth
会导致您的代码无法检测移动浏览器中的滚动条,在那里滚动条的宽度通常为 0。它们只是暂时显示为覆盖层,并不占据文档空间。在移动设备上缩放也变成了一个问题(长话短说)。html
和 body
元素的溢出值为非默认值时,检测可能会出错(然后会发生什么有点复杂-请参见此说明)。.hasScrollbar
方法。如果您愿意,请查看源码。window.scrollbars
对象,它有一个单一属性 visible
,听起来很有希望,但事实并非如此。它不支持IE,包括IE11。而且它只告诉你有一个滚动条——但不是哪个滚动条。请参见这里的测试页面。 - hashchangelet hasScrollbar = window.innerWidth > document.documentElement.clientWidth;
这个对我起作用了:
function hasVerticalScroll(node){
if(node == undefined){
if(window.innerHeight){
return document.body.offsetHeight> window.innerHeight;
}
else {
return document.documentElement.scrollHeight >
document.documentElement.offsetHeight ||
document.body.scrollHeight>document.body.offsetHeight;
}
}
else {
return node.scrollHeight> node.offsetHeight;
}
}
关于页面主体,只需使用hasVerticalScroll()
函数。
clientHeight
比offsetHeight
更可取。否则,当有滚动条时,您可能会有一个粗边框,并返回没有滚动条。 - theicfire <script>
var scrollHeight = document.body.scrollHeight;
var clientHeight = document.documentElement.clientHeight;
var hasVerticalScrollbar = scrollHeight > clientHeight;
alert(scrollHeight + " and " + clientHeight); //for checking / debugging.
alert("hasVerticalScrollbar is " + hasVerticalScrollbar + "."); //for checking / debugging.
</script>
奇怪的是,这些解决方案都没有告诉你页面是否有垂直滚动条。
window.innerWidth - document.body.clientWidth
将给出滚动条的宽度。这在IE9+(未在更低版本浏览器中测试)中应该有效。(或者严格回答问题,!!(window.innerWidth - document.body.clientWidth)
为什么呢?假设您有一个页面,其内容高度超过窗口高度,用户可以向上/向下滚动。如果您在Mac上使用Chrome而没有插入鼠标,则用户将看不到滚动条。插入鼠标,滚动条将出现。(请注意,此行为可以被覆盖,但我认为这是默认行为)。
var hasScrollbar = function() {
// The Modern solution
if (typeof window.innerWidth === 'number')
return window.innerWidth > document.documentElement.clientWidth
// rootElem for quirksmode
var rootElem = document.documentElement || document.body
// Check overflow style property on body for fauxscrollbars
var overflowStyle
if (typeof rootElem.currentStyle !== 'undefined')
overflowStyle = rootElem.currentStyle.overflow
overflowStyle = overflowStyle || window.getComputedStyle(rootElem, '').overflow
// Also need to check the Y axis overflow
var overflowYStyle
if (typeof rootElem.currentStyle !== 'undefined')
overflowYStyle = rootElem.currentStyle.overflowY
overflowYStyle = overflowYStyle || window.getComputedStyle(rootElem, '').overflowY
var contentOverflows = rootElem.scrollHeight > rootElem.clientHeight
var overflowShown = /^(visible|auto)$/.test(overflowStyle) || /^(visible|auto)$/.test(overflowYStyle)
var alwaysShowScroll = overflowStyle === 'scroll' || overflowYStyle === 'scroll'
return (contentOverflows && overflowShown) || (alwaysShowScroll)
}
window.innerWidth>document.documentElement.clientWidth
为真,但是window.innerHeight > document.documentElement.clientHeight
不为真。在这种情况下,它看起来就像是反过来的。我不是一个JS开发人员,我只需要它用于Selenium测试。我感激任何提示。 - kriegaex只需比较文档根元素(即html元素)的宽度与窗口的内部部分:
if ((window.innerWidth - document.documentElement.clientWidth) >0) console.log('V-scrollbar active')
vScrollbarWidth = window.innerWidth - document.documentElement.clientWidth;
overflow-y:scroll
但页面上实际没有溢出的边缘情况。其他检查溢出高度的解决方案无法解决此问题。 - kimbaudi
body
的overflow
属性在某个地方设置为hidden
,它将无效。不过,在body
上设置隐藏非常罕见。 - Pekka$("body")
无法使用时,我不得不使用$(document)
,这对我起作用了(我有一个宽高比上的绝对定位容器)。 - am_