检测iframe内容高度变化 [英] Detect iframe content height change

查看:425
本文介绍了检测iframe内容高度变化的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大量示例显示了如何动态设置 iframe 的内容高度。这对我来说很完美。我现在遇到的问题是内容可以改变大小而不会触发 onload (想想隐藏/可扩展 div s )。

There are plenty of examples showing how to dynamically set an iframe's height to its content. This works perfect for me. The problem I'm now having is that the content can change size without triggering onload (think hidden/expandable divs).

有没有办法检测 iframe 内容的大小何时发生变化?这是在同一个域上,没有jQuery,请。

Is there any way to detect when the size of the iframe content has changed? This is on the same domain and no jQuery, please.

推荐答案

我会通过定期轮询(也许每200毫秒,或许更经常地)使用 setInterval 。然后,您可以将内容大小与上次内容进行比较。

I would do this by polling regularly (maybe every 200 milliseconds, perhaps more often) using setInterval. You could then compare the size of the content to what it was last time.

var iframe = document.getElementById('myIframe'),
    lastheight;

setInterval(function(){
    if (iframe.document.body.scrollheight != lastheight) {
        // adjust the iframe's size

        lastheight = iframe.document.body.scrollheight;
    }
}, 200);

这篇关于检测iframe内容高度变化的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆