1

所以基本上我有一个单页网站,里面有一大堆隐藏的 Vimeo 视频。我需要所有视频在未显示时暂停。

到目前为止,我可以在单击特定锚标签时暂停一个视频,但我希望能够通过单击任何 a 标签来暂停所有视频。

我尝试了很多不同的东西,但由于我对 JavaScript 的了解有限,我觉得我已经超出了我的深度。

任何指针将不胜感激。

<script>
function showonlyone(thechosenone) {
var newboxes = document.getElementsByTagName("div");
for(var x=0; x<newboxes.length; x++) {
name = newboxes[x].getAttribute("name");
if (name == 'newboxes') {
if (newboxes[x].id == thechosenone) {
newboxes[x].style.display = 'block';
}
else {
newboxes[x].style.display = 'none';
}}}}
</script>

<div name="newboxes" id="newboxes0" style="display: block;">
<iframe id="player" src="http://player.vimeo.com/video/53407474?api=1&player_id=player" width="800" height="450" frameborder="0" webkitAllowFullScreen mozallowfullscreen allowFullScreen></iframe>
</div>
<div name="newboxes" id="newboxes0" style="display: none;">
<iframe id="player" src="http://player.vimeo.com/video/53855813?api=1&player_id=player" width="800" height="450" frameborder="0" webkitAllowFullScreen mozallowfullscreen allowFullScreen></iframe>
</div>
<div name="newboxes" id="newboxes0" style="display: none;">
<iframe id="player" src="http://player.vimeo.com/video/19780095?api=1&player_id=player" width="800" height="450" frameborder="0" webkitAllowFullScreen mozallowfullscreen allowFullScreen></iframe>
</div>

<a name="button" id="button1" href="javascript:showonlyone('newboxes0');"><span></span></a>
<a name="button" id="button2" href="javascript:showonlyone('newboxes1');"><span></span></a>
<a name="button" id="button3" href="javascript:showonlyone('newboxes2');"><span></span></a>

<script src="http://a.vimeocdn.com/js/froogaloop2.min.js"></script>
<script>
function ready(player_id) {
var player = $f(player_id);
pauseButton = document.getElementById('button1');
pauseButton.addEventListener('click', function() {
player.api('pause');
});}
window.addEventListener('load', function() {
$f(document.getElementById('player')).addEvent('ready', ready);
});
</script>
4

1 回答 1

1

好吧,我在后面注意到的一件事是您有多个相同的 ID。ID 应该是唯一的。查看 jsFiddle

所以我把你的 html 改写成这样

<div name="newboxes" id="newboxes0" style="display: block;">
    <iframe id="player0" src="http://player.vimeo.com/video/53407474?api=1&player_id=player" width="800" height="450" frameborder="0" webkitAllowFullScreen mozallowfullscreen allowFullScreen></iframe>
</div>
<div name="newboxes" id="newboxes1" style="display: none;">
    <iframe id="player1" src="http://player.vimeo.com/video/53855813?api=1&player_id=player" width="800" height="450" frameborder="0" webkitAllowFullScreen mozallowfullscreen allowFullScreen></iframe>
</div>
<div name="newboxes" id="newboxes2" style="display: none;">
    <iframe id="player2" src="http://player.vimeo.com/video/19780095?api=1&player_id=player" width="800" height="450" frameborder="0" webkitAllowFullScreen mozallowfullscreen allowFullScreen></iframe>
</div>

<a name="button" id="button0" href="javascript:showonlyone('newboxes0');"><span>Button1</span></a>
<a name="button" id="button1" href="javascript:showonlyone('newboxes1');"><span>Button2</span></a>
<a name="button" id="button2" href="javascript:showonlyone('newboxes2');"><span>Button3</span></a>​

比我更新你的 JavaScript 看起来像这样

function showonlyone(thechosenone) {
    var newboxes = document.getElementsByName("newboxes"), x=0;

    for(var x = 0; x < newboxes.length; x++) {
        if (newboxes[x].id == thechosenone) {
            newboxes[x].style.display = 'block'; 
            var strPlayerID = newboxes[x].id.replace("newboxes", "player"); 
            var iframe = $('#'+strPlayerID)[0], player = $f(iframe);
            player.api('play');  
        } else {
            newboxes[x].style.display = 'none'; 
            var strPlayerID = newboxes[x].id.replace("newboxes", "player"); 
            var iframe = $('#'+strPlayerID)[0], player = $f(iframe);
            player.api('pause'); 
        }
    }
}

希望这可以帮助!

于 2012-11-21T16:07:56.697 回答