Ruby's Louvre

每天学习一点点算法

导航

iframe高度自适应


            var adjustIframe = function(id, end) {
                var iframe = document.getElementById(id), time = 0, end = end || 30, intervalID
                if (iframe) {
                    function callback() {
                        time = time + 1
                        if (time == end) {
                            clearInterval(intervalID)
                        }
                        var idoc = iframe.contentWindow && iframe.contentWindow.document || iframe.contentDocument;
                        var iheight = Math.max(idoc.body.scrollHeight, idoc.documentElement.scrollHeight); //取得其高
                        iframe.style.height = iheight + "px";
                    }
                    intervalID = setInterval(callback, 50)
                }
            }
<iframe id="js_sub_web" width="80%" frameborder="0" scrolling="no" allowTransparency="true" src="http://www.cnblogs.com/rubylouvre/archive/2009/09/15/1566722.html">
</iframe>

使用方法:

window.onload = function(){
     adjustIframe("js_sub_web");
}

注意,本脚本只能处理同域的iframe高度

示例

posted on 2011-01-05 14:13  司徒正美  阅读(3751)  评论(7编辑  收藏  举报