Skip to content

Instantly share code, notes, and snippets.

@Patrick64
Created June 4, 2019 10:40
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Patrick64/942daecfb34735ae0cc4474a421b3f93 to your computer and use it in GitHub Desktop.
Save Patrick64/942daecfb34735ae0cc4474a421b3f93 to your computer and use it in GitHub Desktop.
function resizeVideos() {
var $allVideos = $("iframe[src^='https://player.vimeo.com'], iframe[src^='https://www.youtube.com'], object, embed");
$allVideos.each(function() {
$(this)
// jQuery .data does not work on object/embed elements
.attr('data-aspectRatio', this.height / this.width)
.removeAttr('height')
.removeAttr('width');
});
$(window).resize(function() {
$allVideos.each(function() {
var newWidth = $(this).parent().width();
var $el = $(this);
$el
.width(newWidth)
.height(newWidth * $el.attr('data-aspectRatio'));
});
}).resize();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment