/ Published in: JavaScript
JavaScript code to facilitate dynamic re-sizing of an iframe at the same time as the browser window is re-sized, as well as making sure that the frame scales proportionally to its width and height.
Expand |
Embed | Plain Text
Copy this code and paste it in your HTML
<script type="text/javascript"> $.noConflict(); function adjustIframes() { $('iframe').each(function(){ var $this = $(this), proportion = $this.data( 'proportion' ), w = $this.attr('width'), actual_w = $this.width(); if ( ! proportion ) { proportion = $this.attr('height') / w; $this.data( 'proportion', proportion ); } if ( actual_w != w ) { var h = Math.round( actual_w * proportion ); if (!isNaN(h)) { if (console && console.log) console.log("Resizing iframe to height: " + h); $this.css( 'height', h + 'px' ); if ($this.get(0).contentWindow.postMessage) { $this.get(0).contentWindow.postMessage('resize', 'your website here'); } } } }); } $(window).on('resize load',adjustIframes); }); </script>
URL: javascript, jquery, iframe