iframe Auto Adjust Height as content changes

前端 未结 4 1309
無奈伤痛
無奈伤痛 2020-12-01 09:36

I have an iframe as you can see on the following link;-

http://one2onecars.com

The iframe is the online booking in the centre of the screen. The problem I ha

4条回答
  •  攒了一身酷
    2020-12-01 09:59

    I wrote this script and it's working perfectly for me. Feel free to use it!

    function ResizeIframeFromParent(id) {
        if (jQuery('#'+id).length > 0) {
            var window = document.getElementById(id).contentWindow;
            var prevheight = jQuery('#'+id).attr('height');
            var newheight = Math.max( window.document.body.scrollHeight, window.document.body.offsetHeight, window.document.documentElement.clientHeight, window.document.documentElement.scrollHeight, window.document.documentElement.offsetHeight );
            if (newheight != prevheight && newheight > 0) {
                jQuery('#'+id).attr('height', newheight);
                console.log("Adjusting iframe height for "+id+": " +prevheight+"px => "+newheight+"px");
            }
        }
    }
    

    You can call the function inside a loop:

    
    

提交回复
热议问题