繁体   English   中英

检测iFrame JS中的滚动条

[英]Detect scrollbars within iFrame JS

我的网页上有一个嵌入式iFrame,该网页中使用的是第三方工具,这意味着来自iFrame的URL来自其他位置。

调整页面大小时,我需要在iFrame窗口中检测滚动条的呈现,然后在检测到任务后执行任务。

我尝试了各种不同的解决方案,但都没有成功。

这可能吗?

非常感谢!

这是我想到的第一件事: http : //jsfiddle.net/matias/hhcKn/

只是示例代码!

HTML:

<div id="body"></div>

JS:

var $iframe = $("<iframe></iframe>").appendTo("#body");
var iframe = $iframe[0];
var doc = iframe.document;

//test this
var content = "<h1>Hello world</h1><br><p>more content!</p>";

//and then this
//var content = "<h1>Hello world</h1><br><br><br><br><br><p>more content!</p>";

if(iframe.contentDocument){
    doc = iframe.contentDocument;
}
else if(iframe.contentWindow){
    doc = iframe.contentWindow.document;
}
doc.open();
doc.writeln(content);
doc.close();


//this is the part that might interest you
var div_height = $("#body").height();
var iframe_height = $("iframe").contents().height();

if(iframe_height > div_height) alert("scrollbars present");

CSS:

body{
    border: 1px solid red;
}

iframe{
    border: none;
}
<iframe src="111.html" id="iframeid" height="300" width="800"></iframe>

<script type="text/javascript">
function resizeIFrame(){
    $("#iframeid").attr("height", $("#iframeid").contents().height());
}
setInterval("resizeIFrame()", 500)
</script>

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM