Quantcast
Channel: Resizing an iframe based on content - Stack Overflow
Viewing all articles
Browse latest Browse all 28

Answer by hanshenrik for Resizing an iframe based on content

$
0
0

couldn't find something that perfectly handled large texts + large images, but i ended up with this, seems this gets it right, or nearly right, every single time:

    iframe.addEventListener("load",function(){        // inlineSize, length, perspectiveOrigin, width        let heightMax = 0;        // this seems to work best with images...        heightMax = Math.max(heightMax,iframe.contentWindow.getComputedStyle(iframe.contentWindow.document.body).perspectiveOrigin.split("px")[0]);        // this seems to work best with text...        heightMax = Math.max(heightMax,iframe.contentWindow.document.body.scrollHeight);        // some large 1920x1080 images always gets a little bit off on firefox =/        const isFirefox = navigator.userAgent.toLowerCase().indexOf('firefox') > -1;        if(isFirefox && heightMax >= 900){            // grrr..            heightMax = heightMax + 100;        }        iframe.style.height = heightMax+"px";        //console.log(heightMax);    });

Viewing all articles
Browse latest Browse all 28

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>