kodie
4/12/2016 - 3:10 PM

Make YouTube video embeds responsive

Make YouTube video embeds responsive

var responsiveVideos = $('iframe[src*="//www.youtube.com"]');

responsiveVideos.each(function(){
  $(this)
    .data('aspectRatio', $(this).height() / $(this).width())
    .width('100%')
    .removeAttr('height');
});

$(window).resize(function(){
  responsiveVideos.each(function(){
    $(this)
      .height($(this).width() * $(this).data('aspectRatio'));
  });
}).resize();