Skip to content

Instantly share code, notes, and snippets.

@Phocacius
Created December 4, 2016 00:36
Show Gist options
  • Save Phocacius/c28c828d4171f6d030cedc62b9a4485b to your computer and use it in GitHub Desktop.
Save Phocacius/c28c828d4171f6d030cedc62b9a4485b to your computer and use it in GitHub Desktop.
automatically adapts the width of an iframe to the available width, needs ratio set via width/height attributes and width set to 100% via CSS
/* Responsive iFrames
- - - - - - - - - - - - - - - - - - - - */
var $iframes = $('.iframe-responsive');
if($iframes.length) {
var adaptIframes = function() {
$iframes.each(function(index, item) {
var $iframe = $(item);
var ratio = $iframes.attr('width') / $iframes.width();
$iframe.css('height', $iframes.attr('height') / ratio);
});
};
adaptIframes();
$(window).on('resize', adaptIframes);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment