Created
May 1, 2012 15:52
-
-
Save rhyolight/2569053 to your computer and use it in GitHub Desktop.
JavaScript for youtube thumbnail overlay
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
(function() { | |
var $overlay = $('#image-overlay'), | |
// NYAN CAT!! | |
youtubeVideoId = 'QH2-TGUlwu4', | |
// id of the element the object embed will replace | |
playerPlaceholderId = 'video-placeholder', | |
// id we want to give to the new <object> | |
playerId = 'video-player', | |
// shockwave flash object params | |
params = { | |
allowScriptAccess: 'always', | |
wmode: 'transparent' | |
}, | |
// shockwave flash object attributes | |
attrs = { | |
id: playerId | |
}, | |
// object we'll call playeVideo() on once it is loaded | |
playerApi, | |
// function YouTube will call when the player is ready | |
playerReady, | |
// function called when player state changes (play/pause/stop/end/etc) | |
playerStateChange; | |
// defining the callback fuction to be called when player is on the page | |
// and ready to play | |
playerReady = function() { | |
var $player = $('#' + playerId), | |
playerOffsets = $player.offset(); | |
playerApi = $player[0]; | |
// scoot the overlay image into place over the swf object embed | |
$overlay.css({ | |
left: playerOffsets.left, | |
top: playerOffsets.top | |
}); | |
// on overlay click, hide the overlay and play the video | |
$overlay.click(function() { | |
$overlay.hide(); | |
playerApi.playVideo(); | |
}); | |
// when video is done playing, put the overlay back | |
playerApi.addEventListener('onStateChange', 'onYouTubePlayerStateChange'); | |
}; | |
playerStateChange = function(event) { | |
if (event === 0) { | |
$overlay.show(); | |
} | |
}; | |
// see http://code.google.com/p/swfobject/wiki/api for details | |
// (I don't really care about the details. ;) ) | |
swfobject.embedSWF( | |
// URL of video | |
'http://www.youtube.com/v/' + youtubeVideoId + '?enablejsapi=1&playerapiid=ytplayer&version=3&rel=0', | |
// id to put the object embed within | |
playerPlaceholderId, | |
// width | |
'500', | |
// height | |
'500', | |
// don't know | |
'8', | |
// don't care | |
null, | |
// not sure | |
null, | |
// params and attrs defined above | |
params, | |
attrs | |
); | |
// export global functions youtube expects to call | |
window.onYouTubePlayerReady = playerReady; | |
window.onYouTubePlayerStateChange = playerStateChange; | |
})(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment