Skip to content

Instantly share code, notes, and snippets.

@tammyhart
Last active August 19, 2018 11:10
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 3 You must be signed in to fork a gist
  • Save tammyhart/6199503 to your computer and use it in GitHub Desktop.
Save tammyhart/6199503 to your computer and use it in GitHub Desktop.
Make iframes responsive by calculating the correct aspect ratio and filling to the width of the container as it flexes.
// iframes
var $iframes = $( document.querySelectorAll( '.videos iframe' ) );
function iframeSizing() {
var $this = $( this ),
width = $this.width(),
height = $this.height(),
containerWidth = $this.parent().width(),
heightRatio = height / width,
newHeight = heightRatio * containerWidth;
$this.width( containerWidth );
$this.height( newHeight );
}
function iframesEachSizing() {
$iframes.each( iframeSizing );
}
iframesEachSizing();
$( window ).resize( iframesEachSizing );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment