Created
July 8, 2013 20:16
-
-
Save jtallant/5952118 to your computer and use it in GitHub Desktop.
Responsive iframe embeds
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* 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); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Don't forget to set iframe {width: 100%} in CSS