Created
April 10, 2013 19:53
-
-
Save cdnsteve/5357886 to your computer and use it in GitHub Desktop.
Responsive YouTube or Vimeo vids from http://toddmotto.com/fluid-and-responsive-youtube-and-vimeo-videos-with-fluidvids-js/
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
(function() { | |
var iframes = document.getElementsByTagName('iframe'); | |
for (var i = 0; i < iframes.length; i++) { | |
var iframe = iframes[i]; | |
var players = /www.youtube.com|player.vimeo.com/; | |
if(iframe.src.search(players) !== -1) { | |
var videoRatio = (iframe.height / iframe.width) * 100; | |
iframe.style.position = 'absolute'; | |
iframe.style.top = '0'; | |
iframe.style.left = '0'; | |
iframe.width = '100%'; | |
iframe.height = '100%'; | |
var div = document.createElement('div'); | |
div.className = 'video-wrap'; | |
div.style.width = '100%'; | |
div.style.position = 'relative'; | |
div.style.paddingTop = videoRatio + '%'; | |
var parentNode = iframe.parentNode; | |
parentNode.insertBefore(div, iframe); | |
div.appendChild(iframe); | |
} | |
} | |
})(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment