【发布时间】:2018-07-22 06:14:00
【问题描述】:
我正在尝试使用 Youtube API 在投资组合风格的网站上维护多个 Youtube 视频。我想让每个视频都包含在一个模态中,这样当单击缩略图时,模态就会打开,并且里面的视频会自动开始播放。
我引用了这个 codepen 来通过它们的 ID 获取多个 iframe,我使用了一个 click 事件并将其绑定到模态关闭按钮以停止所有视频播放实例。一旦模式关闭,这将阻止任何视频继续播放。但我不知道如何在每次点击的基础上开始播放每个视频。
参考 Codepen:https://codepen.io/AliKlein/pen/WoNaoN
使用 Zurb 基金会构建此网站
<div class="video-thumbnail">
<div class="video-play show-in-overlay">
<a data-toggle="featvideo">
<svg class="icon" xmlns="http://www.w3.org/2000/svg"
viewBox="0 0 512 512"><path id="Video-Play" d="M96 64l320 192-320
192z"></path>
</svg>
</a>
</div>
<div class="media-item-caption">
<h1 class="media-item-caption-title">Title</h1>
<div class="media-caption-sub"><p>Caption</p>
</div>
</div>
<img src="Video1thumb.jpeg">
</div><!-- "video-thumbnail" -->
<div class="full reveal" id="featvideo" data-reveal>
<div class="responsive-embed widescreen reveal-content">
<div id="player1">
[IFRAME ENDS UP HERE]
</div>
</div>
<button class="close-button" data-close aria-label="Close reveal"
type="button">
<span aria-hidden="true">×</span>
</button>
</div>
<div class="video-thumbnail">
<div class="video-play show-in-overlay">
<a data-toggle="secondvideo">
<svg class="icon" xmlns="http://www.w3.org/2000/svg"
viewBox="0 0 512 512"><path id="Video-Play" d="M96 64l320 192-320
192z"></path>
</svg>
</a>
</div>
<div class="media-item-caption">
<h1 class="media-item-caption-title">Second
Title</h1>
<div class="media-caption-sub"><p>Second Caption</p>
</div>
</div>
<img src="Video2thumb.jpeg">
</div><!-- "video-thumbnail" -->
<div class="full reveal" id="featvideo" data-reveal>
<div class="responsive-embed widescreen reveal-content">
<div id="player2">
[SECOND IFRAME ENDS UP HERE]
</div>
</div>
<button class="close-button" data-close aria-label="Close reveal"
type="button">
<span aria-hidden="true">×</span>
</button>
</div>
</html>
<script>
var tag = document.createElement('script');
tag.src = "https://www.youtube.com/iframe_api";
var firstScriptTag = document.getElementsByTagName('script')[0];
firstScriptTag.parentNode.insertBefore(tag, firstScriptTag);
var playerInfoList = [{
id: 'player1',
height: '175',
width: '300',
videoId: 'dOy7vPwEtCw'
}, {
id: 'player2',
height: '175',
width: '300',
videoId: 'QWtsV50_-p4'
}, {
id: 'player3',
height: '175',
width: '300',
videoId: 'y-JqH1M4Ya8'
}, {
id: 'player4',
height: '175',
width: '300',
videoId: 'gH7dMBcg-gE'
}, {
id: 'player5',
height: '175',
width: '300',
videoId: '7wL9NUZRZ4I'
}, {
id: 'player6',
height: '175',
width: '300',
videoId: 'S4R8HTIgHUU'
}];
function onYouTubeIframeAPIReady() {
if (typeof playerInfoList === 'undefined') return;
for (var i = 0; i < playerInfoList.length; i++) {
var curplayer = createPlayer(playerInfoList[i]);
players[i] = curplayer;
}
}
var players = new Array();
function createPlayer(playerInfo) {
return new YT.Player(playerInfo.id, {
height: playerInfo.height,
width: playerInfo.width,
videoId: playerInfo.videoId,
});
}
$('.close-button').click(function () {
$(players).each(function (i) {
this.stopVideo();
});
});
</script>
【问题讨论】:
标签: jquery youtube youtube-api zurb-foundation youtube-iframe-api