我正在使用 youtube api 添加很多视频,比在这个例子中更多,一页 15 个,另一页 30 个;逻辑是相同的
// load api
var tag = document.createElement('script');
tag.src = "//www.youtube.com/iframe_api";
var firstScriptTag = document.getElementsByTagName('script')[0];
firstScriptTag.parentNode.insertBefore(tag, firstScriptTag);
// make player array
var players = new Array();
function onYouTubeIframeAPIReady() {
players[0] = new YT.Player('player1', { //player1 is a div id in the html, this is how it works throughout the page
height: '405',
width: '720',
videoId: 'Sqd3jUPq3Lw',
playerVars: {
'rel': 0,
'enablejsapi': 1,
'iv_load_policy': 3
}
});
players[1] = new YT.Player('player2', {
height: '315',
width: '560',
videoId: 'Sqd3jUPq3Lw',
suggestedQuality: 'large',
iv_load_policy: '3',
playerVars: {
'rel': 0,
'enablejsapi': 1,
'iv_load_policy': 3
}
});
players[2] = new YT.Player('player3', {
height: '315',
width: '560',
videoId: 'Sqd3jUPq3Lw',
suggestedQuality: 'large',
iv_load_policy: '3',
playerVars: {
'rel': 0,
'enablejsapi': 1,
'iv_load_policy': 3
}
});
players[3] = new YT.Player('player4', {
height: '315',
width: '560',
videoId: 'Sqd3jUPq3Lw',
iv_load_policy: '3',
playerVars: {
'rel': 0,
'enablejsapi': 1,
'iv_load_policy': 3
}
});
players[4] = new YT.Player('player5', {
height: '315',
width: '560',
videoId: 'Sqd3jUPq3Lw',
playerVars: {
'rel': 0,
'enablejsapi': 1,
'iv_load_policy': 3
}
});
players[5] = new YT.Player('player6', {
height: '315',
width: '560',
videoId: 'Sqd3jUPq3Lw',
playerVars: {
'rel': 0,
'enablejsapi': 1,
'iv_load_policy': 3
}
});
players[6] = new YT.Player('player7', {
height: '315',
width: '560',
videoId: 'Sqd3jUPq3Lw',
playerVars: {
'rel': 0,
'enablejsapi': 1,
'iv_load_policy': 3
}
});
}
//to stop the video from playing
$(document).click( function() {
//loop players array to stop them all
$(players).each(function(i){
this.pauseVideo();
});
});
上述代码的问题在于 iframe 需要很长时间才能加载到页面上。这会干扰其他 javascript,例如触发包含某些视频的模式窗口。
在下一个示例中,我将视频分成 3 个数组,其中 2 个在诸如模态按钮事件之类的事件上触发。
// load api
var tag = document.createElement('script');
tag.src = "//www.youtube.com/iframe_api";
var firstScriptTag = document.getElementsByTagName('script')[0];
firstScriptTag.parentNode.insertBefore(tag, firstScriptTag);
// make player array
var players = new Array();
var players2 = new Array();
var players3 = new Array();
function onYouTubeIframeAPIReady() {
players[0] = new YT.Player('player1', {
height: '405',
width: '720',
videoId: 'Sqd3jUPq3Lw',
playerVars: {
'rel': 0,
'enablejsapi': 1,
'iv_load_policy': 3
}
});
$('.btn-1').click( function(){
players2[0] = new YT.Player('player3', {
height: '315',
width: '560',
videoId: 'Sqd3jUPq3Lw',
suggestedQuality: 'large',
iv_load_policy: '3',
playerVars: {
'rel': 0,
'enablejsapi': 1,
'iv_load_policy': 3
}
});
players2[1] = new YT.Player('player4', {
height: '315',
width: '560',
videoId: 'Sqd3jUPq3Lw',
iv_load_policy: '3',
playerVars: {
'rel': 0,
'enablejsapi': 1,
'iv_load_policy': 3
}
});
players2[2] = new YT.Player('player5', {
height: '315',
width: '560',
videoId: 'Sqd3jUPq3Lw',
playerVars: {
'rel': 0,
'enablejsapi': 1,
'iv_load_policy': 3
}
});
});
$('.btn-2').click( function(){
players3[0] = new YT.Player('player7', {
height: '315',
width: '560',
videoId: 'Sqd3jUPq3Lw',
playerVars: {
'rel': 0,
'enablejsapi': 1,
'iv_load_policy': 3
}
});
players3[1] = new YT.Player('player8', {
height: '315',
width: '560',
videoId: 'Sqd3jUPq3Lw',
playerVars: {
'rel': 0,
'enablejsapi': 1,
'iv_load_policy': 3
}
});
players3[2] = new YT.Player('player9', {
height: '315',
width: '560',
videoId: 'Sqd3jUPq3Lw',
playerVars: {
'rel': 0,
'enablejsapi': 1,
'iv_load_policy': 3
}
});
});
}
$(document).click( function() {
//loop players array to stop them all
$(players).each(function(i){
this.pauseVideo();
});
$(players2).each(function(i){
this.pauseVideo();
});
$(players3).each(function(i){
this.pauseVideo();
});
});
这实际上会错开加载时间。页面加载得很快,虽然模态框需要更长的时间,但我觉得用户愿意再等几秒钟来加载 iframe。
不起作用的是最后一个在文档点击时暂停所有视频的功能(当用户点击离开模式时很有用)。具有讽刺意味的是,如果我摆脱了按钮单击事件,函数 onYouTubeIframeAPIReady 能够为所有三个数组创建 iframe。一旦将这些点击事件添加到函数中,pausevideo 函数就会失败。
我已经阅读了 youtube 文档,但没有找到解决方案。
预先感谢您的帮助。