Skip to content

Instantly share code, notes, and snippets.

@michaelslevy
Forked from aarongustafson/responsive-iframes.css
Last active March 21, 2017 16:02
Show Gist options
  • Save michaelslevy/45936329bd2670c7edfa2a050368a7eb to your computer and use it in GitHub Desktop.
Save michaelslevy/45936329bd2670c7edfa2a050368a7eb to your computer and use it in GitHub Desktop.
Responsive iFrames with jQuery
<!-- All you need is a defined width & height -->
<iframe frameborder="0" height="426" src="http://www.slideshare.net/slideshow/embed_code/9812085?rel=0" width="510"></iframe>
function adjustIframes()
{
$('iframe').each(function(){
var
$this = $(this),
w = $this.attr('width'),
h = $this.attr('height'),
proportion = h / w;
var new_w=$this.parent().width();
var new_h=Math.round( new_w * proportion );
$this.height(new_h);
$this.width(new_w);
});
}
$(window).on('resize load',adjustIframes);
@michaelslevy
Copy link
Author

Removed the CSS to make the JavaScript self-contained.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment