Created
May 19, 2013 15:27
-
-
Save anchoo2kewl/5607991 to your computer and use it in GitHub Desktop.
Just include this code on a page with videos and see them "magically" become responsive.
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
var $allVideos = $("iframe[src^='http://player.vimeo.com'],iframe[src^='http://www.youtube.com']"); | |
var $fluidEl = $("figure"); | |
$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() { | |
var newWidth = $fluidEl.width(); | |
$allVideos.each(function() { | |
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