我真的需要Youtube API的帮助。
<script type="text/javascript" src="http://www.youtube.com/player_api"></script>
<script type='text/javascript'>
window.onload=function(){
var player;
var $ = function(id) {
return document.getElementById(id);
}
var $$ = function(tagname) {
return document.getElementsByTagName(tagname);
}
var $$$ = function(classname) {
return document.getElementsByClassName(classname);
}
function onYouTubeIframeAPIReady() {
var videos = $$('iframe'), // the iframes elements
players = [], // an array where we stock each videos youtube instances class
playingID = null; // stock the current playing video
for (var i = 0; i < videos.length; i++) // for each iframes
{
var currentIframeID = videos[i].id; // we get the iframe ID
players[currentIframeID] = new YT.Player(currentIframeID); // we stock in the array the instance
// note, the key of each array element will be the iframe ID
videos[i].onmouseover = function(e) { // assigning a callback for this event
var currentHoveredElement = e.target;
if (playingID) // if a video is currently played
{
players[playingID].stopVideo();
}
players[currentHoveredElement.id].playVideo();
playingID = currentHoveredElement.id;
};
videos[i].onmouseout = function(e) { // assigning a callback for this event
var currentHoveredElement = e.target;
players[playingID].stopVideo();
playingID = currentHoveredElement.id;
};
}
}
onYouTubeIframeAPIReady();
}>
</script>
这是脚本,对于PHP
$i = 1;
foreach($data['items'] as $child) {
$i++;
<iframe id="player<?php echo $i; ?>" width="270" height="180" data-src="http://www.youtube.com/embed/<?php echo $child['id']['videoId']; ?>?rel=0&wmode=Opaque&enablejsapi=1;showinfo=0;controls=0;autohide=1;modestbranding=1;showsearch=0;" frameborder="0" allowfullscreen style="position:relative;"></iframe>
}
它会显示来自youtube的视频,无论何时你悬停,它都是自动播放,因此视频停止播放。但我想要做的是如果悬停并点击iframe视频,它会转到http://www.google.com/链接。
我该怎么做? 在此先感谢大家。