×

Iframe实现跨浏览器自适应高度解决方法

作者:Terry2017.02.28来源:Web前端之家浏览:4747评论:0
关键词:JQueryJS

本文以实例形式讲述了Iframe实现跨浏览器自适应高度解决方法,非常具有实用价值。分享给大家供大家参考之用。具体方法如下:

该方法使用了jQuery,因此需要在iframe的src页面里面引入jQuery。

父页面里面相对简单一点,主要包含以下代码:

<iframe id="if1" scrolling="no" src="2.html"></iframe>

在iframe的src页面里面代码如下:

<script type="text/javascript">
function resizeContent()
{
    $(window.parent.document).find("#if1").height($("#content").height());
}

function show400()
{
    if($("#test400").css("display") == "none")
    {
       $("#test400").css("display","");
       resizeContent();
    }
    else
    {
       $("#test400").css("display","none");
       resizeContent();
    }
}
$(document).ready(function(){
    resizeContent();
})
</script>
<div id="left111">
 <div class="mnav" onclick="test400()"></div>
 <div class="mnav"><a href="ProductList.html">超级链接</a></div>
 <div class="mnav"><a href="ProductCategory.html">超级链接</a></div>
 <div class="mnav"><a href="ProductCategory.html">超级链接</a></div>
 <div id="test400" style="display:none;height:400px;"></div>
 <div class="mnav"><a href="Orders.html">超级链接</a></div>
 <div class="mnav Mcurrent"><a href="Keywords.html">超级链接</a></div>
 <div class="mnav"><a href="#">超级链接</a></div>
 <div class="mnav"><a href="#">超级链接</a></div>
 <div class="mnav"><a href="#">超级链接</a></div>
 <div class="mnav"><a href="#">超级链接</a></div>
 <div class="mnav"><a href="#">超级链接</a></div>
 <div class="mnav"><a href="#">超级链接</a></div>
 <br />
</div>

注释:

这里面的

$(window.parent.document).find("#if1").height($("#content").height()); 

这句话最初是:

$(window.parent.document).find("#if1").height($(document).height());

可以实现自适应高度,但是不能实现缩放自适应。因为document的高度是展示内容出现过的最高高度,所以这里如果点击展开,iframe不会缩小,只会展示最高出现过的高度

所以这里要用一个父层容器,也就是最新的代码。这样就可以实现自适应了。

相信本文所述对大家的jQuery程序设计有一定的借鉴价值。

您的支持是我们创作的动力!
温馨提示:本文作者系Terry ,经Web前端之家编辑修改或补充,转载请注明出处和本文链接:
https://www.jiangweishan.com/article/svg1488211200668.html

网友评论文明上网理性发言 已有0人参与

发表评论: