使用javascript实现Iframe自适应高度

前端之家收集整理的这篇文章主要介绍了使用javascript实现Iframe自适应高度前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

@H_403_0@方法一:


@H_403_0@<div class="codetitle"><a style="CURSOR: pointer" data="69042" class="copybut" id="copybut69042" onclick="doCopy('code69042')"> 代码如下:
<div class="codebody" id="code69042">
$(window.parent.document).find("#ContentIframe").load(function() {
var main = $(window.parent.document).find("#ContentIframe");
var thisheight = $(document).height();
if (thisheight < 800)
thisheight = 800;
main.height(thisheight);
});

@H_403_0@这种写法,只能对于加载的时候对固有元素的自适应高度,当元素变化的时候(如添加了很多元素,高度变化后)不能及时改变父窗体的iframe高度。

@H_403_0@方法二:

@H_403_0@
代码如下:
@H_403_0@ var main = $(window.parent.document).find("#ContentIframe"); var thisheight = $("body").height(); if (thisheight < 800) { thisheight = 800; } main.height(thisheight+50);

setTimeout(setMainHeight,2000);
}

@H_403_0@添加一个计时器,轮询判断子页面的高度变化。

@H_403_0@以上2种都可以是iframe实现高度自适应,小伙伴们根据自己的项目需求,自由选择吧

猜你在找的JavaScript相关文章