Skip to content

Instantly share code, notes, and snippets.

@jtallant
Created July 8, 2013 20:16
Show Gist options
  • Save jtallant/5952118 to your computer and use it in GitHub Desktop.
Save jtallant/5952118 to your computer and use it in GitHub Desktop.
Responsive iframe embeds
/**
* Responsive iframes
*/
function adjustIframes() {
$('iframe').each(function() {
var $this = $(this),
proportion = $this.data('proportion'),
actual_w = $this.width();
if ( $this.attr('width') === '' ) {
$this.attr('width', 480);
$this.attr('height', 385);
}
var iframe_w = $this.attr('width');
if ( ! proportion ) {
proportion = $this.attr('height') / iframe_w;
$this.data( 'proportion', proportion );
}
if ( actual_w != iframe_w ) {
$this.css( 'height', Math.round( actual_w * proportion ) + 'px' );
}
});
}
$(window).on('resize load', adjustIframes);
@jtallant
Copy link
Author

jtallant commented Jul 8, 2013

Don't forget to set iframe {width: 100%} in CSS

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