使用javascript实现Iframe自适应高度_Javascript教程-查字典教程网
使用javascript实现Iframe自适应高度
使用javascript实现Iframe自适应高度
发布时间:2016-12-30 来源:查字典编辑
摘要:方法一:复制代码代码如下:$(window.parent.document).find("#ContentIframe").load(fun...

方法一:

复制代码 代码如下:

$(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);

});

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

方法二:

复制代码 代码如下:

function setMainHeight() {

var main = $(window.parent.document).find("#ContentIframe");

var thisheight = $("body").height();

if (thisheight < 800) { thisheight = 800; }

main.height(thisheight+50);

setTimeout(setMainHeight, 2000);

}

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

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

相关阅读
推荐文章
猜你喜欢
附近的人在看
推荐阅读
拓展阅读
  • 大家都在看
  • 小编推荐
  • 猜你喜欢
  • 最新Javascript教程学习
    热门Javascript教程学习
    编程开发子分类