Skip to content

Instantly share code, notes, and snippets.

@bergantine
Last active April 14, 2018 20:44
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 bergantine/8142407 to your computer and use it in GitHub Desktop.
Save bergantine/8142407 to your computer and use it in GitHub Desktop.
Fluid-width (responsive) Vimeo and YouTube videos. Adapted from CSS Tricks. http://css-tricks.com/NetMag/FluidWidthVideo/Article-FluidWidthVideo.php
// Find all YouTube videos
var $allVideos = $("iframe[src^='//player.vimeo.com'], iframe[src^='http://www.youtube.com']");
// The element that is fluid width
var $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();
@GuenniWeb
Copy link

GuenniWeb commented Apr 14, 2018

in that line it has to be a comma at the end no semicolon:
var $allVideos = $("iframe[src^='//player.vimeo.com'], iframe[src^='http://www.youtube.com']"),

@GuenniWeb
Copy link

GuenniWeb commented Apr 14, 2018

and for my point of view it is in times where https comes more and more
var $allVideos = $("iframe[src*='player.vimeo.com'], iframe[src*='www.youtube.com']"),

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment