Last active
August 29, 2015 14:19
-
-
Save appsol/fb06403fbe31bd2038ad to your computer and use it in GitHub Desktop.
YouTube iFrame API Playlist
This file contains hidden or 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
/** | |
* WP Video Playlists Plugin Main.js | |
* @version 0.3.0 | |
* @param {window} w | |
* @param {jQuery} $ | |
*/ | |
(function(w, $) { | |
var setup = function setup() | |
{ | |
$(w).resize(function() | |
{ | |
sizeVideoIframe(); | |
}); | |
setupEpisodeList(); | |
$.getScript('https://www.youtube.com/iframe_api'); | |
w.onYouTubeIframeAPIReady = function() | |
{ | |
setupYouTubePlayer(); | |
}; | |
w.videoPlayers = {}; | |
}; | |
var setupEpisodeList = function setupEpisodeList() | |
{ | |
$('.video-episodes').jcarousel({ | |
animation: { | |
duration: 800, | |
easing: 'linear' | |
} | |
}); | |
$('.prev-episode').click(function() | |
{ | |
$('.video-episodes').jcarousel('scroll', '-=1'); | |
}); | |
$('.next-episode').click(function() | |
{ | |
$('.video-episodes').jcarousel('scroll', '+=1'); | |
}); | |
$('.episode-list a').click(function(e) | |
{ | |
e.preventDefault(); | |
var id = $(this).data('youtubeId'); | |
var $player = $(this).parents('.widget,#video_page').find('.video-container'); | |
w.videoPlayers[$player[0].id].loadVideoById({ | |
'videoId': id | |
}); | |
w.videoPlayers[$player[0].id].playVideo(); | |
// var a = $('<a>', {href: $player.attr('src')})[0]; | |
// $player.attr('src', 'http://www.youtube.com/embed/' + id + '?' + a.search); | |
location.href = "#" + $(this).parents('.widget,#video_page').attr('id'); | |
}); | |
}; | |
var setupYouTubePlayer = function setupYouTubePlayer() | |
{ | |
$('.video-container').each(function() | |
{ | |
w.videoPlayers[this.id] = new YT.Player(this); | |
}); | |
sizeVideoIframe(); | |
}; | |
var sizeVideoIframe = function sizeVideoIframe() { | |
$('.video-container').each(function() | |
{ | |
var width = $(this).attr('width'); | |
var height = $(this).attr('height'); | |
if (width !== $(this).parent().width()) { | |
$(this).attr('width', $(this).parent().width()); | |
$(this).attr('height', Math.floor($(this).parent().width() * (height / width))); | |
} | |
}); | |
}; | |
// Initiation point | |
$(document).ready(setup()); | |
})(window, jQuery); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment