2010-02-04 120 views
5

我正在用javascript控制嵌入式YouTube无铬播放器,我想通过设置display:none来偶尔隐藏它。但是,当我再次向玩家展示时,它会丢失其YouTube方法。隐藏的YouTube播放器失去其方法

例如:

<script> 
    swfobject.embedSWF("http://www.youtube.com/apiplayer?enablejsapi=1&playerapiid=player", 
    "player", "425", "356", "8", null, null, 
    {allowScriptAccess: "always"}, {id: 'player'} 
); 

    var player = null; 

    function onYouTubePlayerReady(playerId) { 
    player = document.getElementById(playerId); 
    player.addEventListener('onStateChange', 'playerStateChanged'); 
    } 

    function hidePlayer() { 
    player.pauseVideo(); 
    player.style.display = 'none'; 
    } 
    function showPlayer() { 
    player.style.display = 'block'; 
    player.playVideo(); 
    } 
</script> 
<a href="#" onClick="hidePlayer();">hide</a> 
<a href="#" onClick="showPlayer();">show</a> 
<div id="player"></div> 

调用hidePlayer其次showPlayer提供了有关的playVideo调用此错误:

Uncaught TypeError: Object #<an HTMLObjectElement> has no method 'playVideo' 

我能找到的唯一解决方案是使用visibility:hidden的,但是这是搞乱我的页面布局。那里有其他解决方案吗?

回答

0

我在我的脚本中遇到过这种情况。你可以尝试使它成为1x1像素,而不是完全隐藏它。

这是一个非常烦人的问题,并没有意义,为什么会发生这种情况。

0

Both display:none and visibility:hidden将重新加载播放器,但您可以通过将视频播放器封装在带有overflow:hidden的div中的CSS来解决此问题。

HTML:

<div id="ytwrapper"> 
    <div id="ytplayer"> 
    </div> 
</div> 

CSS:

#ytwrapper { overflow: hidden; } 

JS:

function hidePlayer() { 
    player.pauseVideo(); 
    player.style.marginLeft = "50000px"; 
} 

function showPlayer() { 
    player.style.marginLeft = "0px"; 
    player.playVideo(); 
} 
相关问题