我试图在我的页面加载时加载嵌入的 Youtube 视频,然后在加载后立即隐藏它。这工作正常,但是当我想让它可见时,它只出现一秒钟然后消失。这发生在 Firefox 3.0 和 Safari 4 中,我还没有在任何其他浏览器中尝试过。
我尝试使用 .style.display = 'none' 然后 .style.display = ''; 隐藏它 和 style.display='block'; 使其再次可见,并尝试使用 jQuery .hide() 和 .show() 但这两种方法都会导致 youtube 视频在可见后消失。
是否有一种“正确”的方式我应该使用javascript使 <object>...<embed> 标签隐藏和可见,这样当我尝试使其可见时它不会消失?我不只是在需要时动态加载第二个视频的原因是我希望视频开始下载,以便在我准备好使其可见时立即可以播放。
下面是我的 html 和 javascript 代码片段:
mute()、play()、stop() 只是同名 youtube javascript api 调用的包装器。
switchVideoPlayers() 从 html 按钮调用,分别将 1 和 2 作为 oldid 和 newid 传递。
我不知道我应该包含哪些片段作为片段,所以我将整个页面转储到下面:
<html>
<head>
<title>YouTube test</title>
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.3.2/jquery.min.js"></script>
<script>
var player = new Array();
var currentplayer = 1;
function onYouTubePlayerReady(playerid)
{
if (playerid.indexOf('obj') > -1)
{
playerid = playerid.substring(3);
}
debug("player " + playerid + " loaded");
player[playerid] = document.getElementById(playerid);
if (playerid == 1)
player[playerid].loadVideoById('5hSW67ySCio');
else
{
player[playerid].loadVideoById('VgMVHc5N3WM', 10); //videoid
mute(playerid);
setTimeout(function() {
stop(playerid);
$("#obj" + playerid).hide();
}, 1000);
}
}
function play(id)
{
player[id].playVideo();
}
function pause(id)
{
player[id].pauseVideo();
}
function stop(id)
{
player[id].stopVideo();
}
function mute(id)
{
player[id].mute();
}
function unmute(id)
{
player[id].unMute();
}
function seek(id,seconds)
{
player[id].seekTo(seconds, false);
}
function getCurrentTime(id)
{
return player[id].getCurrentTime();
}
function loadNewVideo(id,videoid, startseconds)
{
player[id].loadVideoById(videoid, startseconds);
mute(id);
setTimeout(function() { stop(id); }, 1000);
}
function switchVideoPlayers(oldid, newid)
{
stop(oldid);
$("#obj" + oldid).hide();
$("#obj" + newid).show();
setTimeout(function() {
unmute(newid);
play(newid);
}, 10);
}
function debug(message)
{
$('#debug').html($('#debug').html() + message + "<br />");
}
</script>
</head>
<body>
<object id='obj1' width="425" height="344">
<param name="movie" value="http://www.youtube.com/v/5hSW67ySCio&hl=en&fs=1&"></param>
<param name="allowFullScreen" value="true"></param><param name="allowscriptaccess" value="always"></param>
<embed id='1' src="http://www.youtube.com/apiplayer?enablejsapi=1&playerapiid=1" type="application/x-shockwave-flash" allowscriptaccess="always" allowfullscreen="true" width="425" height="344"></embed>
</object>
<object id='obj2' width="425" height="344">
<param name="movie" value="http://www.youtube.com/v/5hSW67ySCio&hl=en&fs=1&"></param>
<param name="allowFullScreen" value="true"></param><param name="allowscriptaccess" value="always"></param>
<embed id='2' src="http://www.youtube.com/apiplayer?enablejsapi=1&playerapiid=2" type="application/x-shockwave-flash" allowscriptaccess="always" allowfullscreen="true" width="425" height="344"></embed>
</object>
<br />
<br />
<input type='button' value='Play' onclick='play(currentplayer);' />
<input type='button' value='Pause' onclick='pause(currentplayer);' />
<input type='button' value='Stop' onclick='stop(currentplayer);' />
<input type='button' value='Mute' onclick='mute(currentplayer);' />
<input type='button' value='UnMute' onclick='unmute(currentplayer);' />
<input type='button' value='Seek' onclick='seek(currentplayer,getCurrentTime(currentplayer) + 10);' />
<input type='button' value='Get Current Time' onclick='alert(getCurrentTime(currentplayer));' />
<input type='button' value='load strain video' onclick='loadNewVideo(currentplayer+1,"VgMVHc5N3WM", 10);' />
<input type='button' value='switch to next video' onclick='switchVideoPlayers(currentplayer,currentplayer + 1);' />
<br />
<br />
<a href='http://code.google.com/apis/youtube/js_api_reference.html'>api</a>
<div id='debug'>DEBUG MESSAGES:<br />
</div>
</body>
</html>