如何在HTML iFrame中检测滚动条的存在(使用Javascript)?
如何在HTML iFrame中检测滚动条的存在(使用Javascript) 我已经试过了:如何在HTML iFrame中检测滚动条的存在(使用Javascript)?,javascript,html,dom,iframe,scrollbar,Javascript,Html,Dom,Iframe,Scrollbar,如何在HTML iFrame中检测滚动条的存在(使用Javascript) 我已经试过了: var vHeight = 0; if (document.all) { if (document.documentElement) { vHeight = document.documentElement.clientHeight; } else { vHeight = documen
var vHeight = 0;
if (document.all) {
if (document.documentElement) {
vHeight = document.documentElement.clientHeight;
} else {
vHeight = document.body.clientHeight
}
} else {
vHeight = window.innerHeight;
}
if (document.body.offsetHeight > vHeight) {
//when theres a scrollbar
}else{
//when theres not a scrollbar
}
this.scrollLeft=1;
if (this.scrollLeft>0) {
//when theres a scrollbar
this.scrollLeft=0;
}else{
//when theres not a scrollbar
return false;
}
我也试过:
var vHeight = 0;
if (document.all) {
if (document.documentElement) {
vHeight = document.documentElement.clientHeight;
} else {
vHeight = document.body.clientHeight
}
} else {
vHeight = window.innerHeight;
}
if (document.body.offsetHeight > vHeight) {
//when theres a scrollbar
}else{
//when theres not a scrollbar
}
this.scrollLeft=1;
if (this.scrollLeft>0) {
//when theres a scrollbar
this.scrollLeft=0;
}else{
//when theres not a scrollbar
return false;
}
没有成功
我在DOM Inspector上搜索了javascript对象,但没有找到任何东西
是否可以在javacscript的iframe中检测滚动条的存在
iframe内容来自同一个域 到目前为止没有成功
使用jQuery,您可以比较文档高度、滚动顶部位置和视口高度,这可能会得到您需要的答案 大致如下:
$(window).scroll(function(){
if(isMyStuffScrolling()){
//There is a scroll bar here!
}
});
function isMyStuffScrolling() {
var docHeight = $(document).height();
var scroll = $(window).height() + $(window).scrollTop();
return (docHeight == scroll);
}
我认为,由于JavaScript安全限制,如果iframe内容来自另一个域,则无法实现这一点 编辑: 在这种情况下,给iframe一个name='someframe'和id='someframe2',然后比较frames['someframe'].document.body.offsetWidth和document.getElementById('someframe2')。offsetWidth应该会给出答案
var root= document.compatMode=='BackCompat'? document.body : document.documentElement;
var isVerticalScrollbar= root.scrollHeight>root.clientHeight;
var isHorizontalScrollbar= root.scrollWidth>root.clientWidth;
这将检测是否需要滚动条。对于iframe的默认设置,这与是否存在滚动条相同,但如果滚动条被强制打开或关闭(使用父文档中的“scrolling=”yes“/”no“、或iframe文档中的CSS“overflow:scroll/hidden”),则这可能有所不同。我认为您的第二次尝试是正确的。除此之外,您应该尝试滚动/检查
document.body
,而不是this
$(window).scroll(function(){
if(isMyStuffScrolling()){
//scrolling
}else{
//not scrolling
}
});
function isMyStuffScrolling() {
var docHeight = $(document).height();
var scroll = $(window).height() ;//+ $(window).scrollTop();
if(docHeight > scroll) return true;
else return false;
}
改进了Jon的Winstanley代码我发现它适用于任何元素,至少适用于Chrome:
hasVerticalScrollbar = (element.scrollHeight > element.offsetHeight)
||
(element.scrollHeight > element.clientHeight
使用
宽度
而不是高度
可以检测到相同的水平滚动条。谢谢您的回答,但您的代码仅在我尝试移动滚动条时进行测试。我想在页面加载时测试它。很抱歉,这不是正确的解决方案:(您的代码实际上检查文档是否滚动到底部。这里有一个证据:。我认为bobince的答案是正确的。iframe内容来自同一个域。这应该是答案。为什么您要测试“BackCompat”,而不是在所有compat模式下仅使用documentElement
?@bab当您处于Quirks模式时(IE5 compat)顶级文档滚动条位于body元素上,而不是html。为了进一步简化,您可以使用var root=document.scrollingElement;