make iframe height dynamic based on content inside- JQUERY/Javascript

前端 未结 20 2214
情书的邮戳
情书的邮戳 2020-11-22 07:42

I am loading an aspx web page in an iframe. The content in the Iframe can be of more height than the iframe\'s height. The iframe should not have scroll bars.

I have

20条回答
  •  自闭症患者
    2020-11-22 08:09

    A slightly improved answer to BlueFish...

    function resizeIframe(iframe) {
        var padding = 50;
        if (iframe.contentWindow.document.body.scrollHeight < (window.innerHeight - padding))
            iframe.height = iframe.contentWindow.document.body.scrollHeight + "px";
        else
            iframe.height = (window.innerHeight - padding) + "px";
    }
    

    This takes in consideration the height of the windows screen(browser, phone) which is good for responsive design and iframes that have huge height. Padding represents the padding you want above and below the iframe in the case it goes trough whole screen.

提交回复
热议问题