Created
March 9, 2015 13:06
-
-
Save lotsofcode/850a3bc1b67ab09e9fac to your computer and use it in GitHub Desktop.
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
// Find all YouTube videos | |
var $allVideos = $("iframe[src^='http://www.youtube.com']"), | |
// The element that is fluid width | |
$fluidEl = $("body"); | |
// 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(); |
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
.videoWrapper { | |
position: relative; | |
padding-bottom: 56.25%; /* 16:9 */ | |
padding-top: 25px; | |
height: 0; | |
} | |
.videoWrapper iframe { | |
position: absolute; | |
top: 0; | |
left: 0; | |
width: 100%; | |
height: 100%; | |
} |
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
<div class="videoWrapper"> | |
<!-- Copy & Pasted from YouTube --> | |
<iframe width="560" height="349" src="http://www.youtube.com/embed/n_dZNLr2cME?rel=0&hd=1" frameborder="0" allowfullscreen></iframe> | |
</div> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment