Bon*_*chi 21 html javascript iframe dom scrollbar
如何在HTML iFrame中检测滚动条的存在(使用Javascript)?
我已经尝试过了:
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
}
Run Code Online (Sandbox Code Playgroud)
我也尝试过:
this.scrollLeft=1;
if (this.scrollLeft>0) {
//when theres a scrollbar
this.scrollLeft=0;
}else{
//when theres not a scrollbar
return false;
}
Run Code Online (Sandbox Code Playgroud)
没有成功..
我在DOM Inspector上搜索了javascript objets,但没有找到任何内容.
是否可以在javacscript中检测iframe中的滚动条存在?
iframe内容来自同一个域.
直到现在都没有成功..
bob*_*nce 42
var root= document.compatMode=='BackCompat'? document.body : document.documentElement;
var isVerticalScrollbar= root.scrollHeight>root.clientHeight;
var isHorizontalScrollbar= root.scrollWidth>root.clientWidth;
Run Code Online (Sandbox Code Playgroud)
这会检测是否需要滚动条.对于I帧的默认,这是一样的是否是一个滚动的,但如果滚动条被强制开启或关闭(使用父文档中的"SCROLLING ='是’/'否’"属性或CSS"溢出:滚动/隐藏'在iframe文档中)然后这可能会有所不同.
使用jQuery,您可以比较文档高度,scrollTop位置和视口高度,这可能会为您提供所需的答案.
有点像:
$(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);
}
Run Code Online (Sandbox Code Playgroud)