Created
November 11, 2014 12:55
-
-
Save bratsun/364f63459a772123d297 to your computer and use it in GitHub Desktop.
Turn Vimeo or YouTube URL into embedded video
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
// vimeo links | |
$('a[href*="vimeo.com"]').each(function() { | |
$this = $(this); | |
var href = '//player.vimeo.com/video/' + $this.attr('href').split('/').pop(); | |
$this.before('<div class="video-embed"><iframe src="//player.vimeo.com/video/'+$this.attr('href').split('/').pop()+'?autoplay=0&title=0&byline=0&color=1A9691&portrait=0&hd_off=0" width="600" height="337.5" frameborder="0" webkitallowfullscreen mozallowfullscreen allowfullscreen></iframe></div>'); | |
}); | |
// youtube links | |
function getVideoId(url){ | |
if(url.indexOf('?') != -1 ) { | |
var query = decodeURI(url).split('?')[1]; | |
//console.log(query); | |
var params = query.split('&'); | |
for(var i=0,l = params.length;i<l;i++) | |
if(params[i].indexOf('v=') === 0) | |
return params[i].replace('v=',''); | |
} else if (url.indexOf('youtu.be') != -1) { | |
return decodeURI(url).split('youtu.be/')[1]; | |
} | |
return null; | |
} | |
$('a[href*="youtube.com"]').each(function() { | |
var t = $(this); | |
var yid = getVideoId(t.attr('href')); | |
t.before('<div class="video-embed"><iframe src="http://www.youtube.com/embed/' + yid +'?rel=0&autoplay=0" width="600" height="337.5" frameborder="0" webkitallowfullscreen mozallowfullscreen allowfullscreen></iframe></div>'); | |
}); |
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
.video-embed { | |
height: 0; | |
padding-bottom: 50.5% !important; | |
padding-top: 25px; | |
position: relative; | |
width: 100% !important; | |
} | |
.video-embed iframe { | |
position: absolute; | |
top: 0; | |
width: 100% !important; | |
height: 100% !important; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment