Skip to content

Instantly share code, notes, and snippets.

@benwo
Created January 9, 2015 00:30
Show Gist options
  • Save benwo/700e77f8a4071777a590 to your computer and use it in GitHub Desktop.
Save benwo/700e77f8a4071777a590 to your computer and use it in GitHub Desktop.
Fluid Video Embeds
<script>
// Find all videos
var $allVideos = $("iframe[src^='http://player.vimeo.com'], iframe[src^='http://www.youtube.com'], iframe[src^='https://www.youtube.com']"),
// The element that is fluid width, aka the parent element of the video
$fluidEl = $("#content-platter");
// Figure out and save aspect ratio for each video
$allVideos.each(function() {
$(this)
.data('aspectRatio', this.height / this.width)
// and remove the hard coded width/height
.removeAttr('height')
.removeAttr('width');
});
// When the window is resized
$(window).resize(function() {
var newWidth = $fluidEl.width();
// Resize all videos according to their own aspect ratio
$allVideos.each(function() {
var $el = $(this);
$el
.width(newWidth)
.height(newWidth * $el.data('aspectRatio'));
});
// Kick off one resize to fix all videos on page load
}).resize();
</script>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment