Adjust width and height of iframe to fit with content in it

前端 未结 30 2739
旧时难觅i
旧时难觅i 2020-11-22 02:09

I need a solution for auto-adjusting the width and height of an iframe to barely fit its content. The point is that t

30条回答
  •  一整个雨季
    2020-11-22 02:49

    I am using this code to autoadjust height of all iframes (with class autoHeight) when they loads on page. Tested and it works in IE, FF, Chrome, Safari and Opera.

    function doIframe() {
        var $iframes = $("iframe.autoHeight"); 
        $iframes.each(function() {
            var iframe = this;
            $(iframe).load(function() {
                setHeight(iframe);
            });
        });
    }
    
    function setHeight(e) {
      e.height = e.contentWindow.document.body.scrollHeight + 35;
    }
    
    $(window).load(function() {
        doIframe();
    });
    

提交回复
热议问题