-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfluidYoutubes.js
43 lines (30 loc) · 1.09 KB
/
fluidYoutubes.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
//Fluid youtube embeds
//Adapt all video in page with width/height of given div/element
jQuery.fn.fluidYoutubes = function() {
// Find all YouTube videos
var $allVideos = jQuery("iframe[src^='//www.youtube.com']"),
// The element that is fluid width
$fluidEl = this;
// Figure out and save aspect ratio for each video
$allVideos.each(function() {
jQuery(this)
.data('aspectRatio', this.height / this.width)
// and remove the hard coded width/height
.removeAttr('height')
.removeAttr('width');
});
// When the window is resized
// (You'll probably want to debounce this)
jQuery(window).resize(function() {
var newWidth = $fluidEl.width();
// Resize all videos according to their own aspect ratio
$allVideos.each(function() {
var $el = jQuery(this);
$el
.width(newWidth)
.height(newWidth * $el.data('aspectRatio'));
});
// Kick off one resize to fix all videos on page load
}).resize();
return this;
};