gpt4 book ai didi

youtube-api - 已克隆的YouTube视频的JavaScript暂停按钮?

转载 作者:行者123 更新时间:2023-12-03 11:17:58 26 4
gpt4 key购买 nike

我已经为YouTube视频设置了暂停按钮。效果很好,但是现在我需要克隆视频。这是由于正在使用的轮播的工作原理以及视频可以在灯箱中显示的原因。

下面是我的代码的简化版本。现在,暂停按钮仅适用于第一个视频。如何制作一个按钮来暂停所有克隆的视频?

http://jsfiddle.net/36vr2kjv/2/

<p class="trigger">Trigger</p>
<p class="trigger2">Trigger2</p>

<div class="player-wrap">
<div id="player"></div>
</div>

<div id="player2"></div>

var player;

$(function() {
// 2. This code loads the IFrame Player API code asynchronously.
var tag = document.createElement('script');

tag.src = "https://www.youtube.com/iframe_api";
var firstScriptTag = document.getElementsByTagName('script')[0];
firstScriptTag.parentNode.insertBefore(tag, firstScriptTag);

// 3. This function creates an <iframe> (and YouTube player)
// after the API code downloads.
onYouTubeIframeAPIReady = function onYouTubeIframeAPIReady() {
player = new YT.Player('player', {
height: '390',
width: '640',
videoId: 'M7lc1UVf-VE',
events: {
'onReady': onPlayerReady,
'onStateChange': onPlayerStateChange
}
});
}

// 4. The API will call this function when the video player is ready.
function onPlayerReady(event) {
event.target.playVideo();
}

// 5. The API calls this function when the player's state changes.
// The function indicates that when playing a video (state=1),
// the player should play for six seconds and then stop.
var done = false;
function onPlayerStateChange(event) {
if (event.data == YT.PlayerState.PLAYING && !done) {
setTimeout(stopVideo, 6000);
done = true;
}
}
function stopVideo() {
player.stopVideo();
}

$('.trigger').click(function(){
//this works
player.pauseVideo()
});

return player;
});

$(function() {
setTimeout(function(){
$('.player-wrap').clone().appendTo('#player2');
}, 2000);
});

$(function() {
$('.trigger2').click(function(){
// this does not work
player.pauseVideo()
});
});

最佳答案

实际上,存在深克隆和浅克隆的概念。浅克隆不会复制事件,因此您必须使用深克隆。在您使用clone()的任何地方,请改用clone(true)。
例如,在上面的代码片段中,您可以使用:

  $(function() {
setTimeout(function(){
$('.player-wrap').clone(true).appendTo('#player2');
}, 2000);
});

关于youtube-api - 已克隆的YouTube视频的JavaScript暂停按钮?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33054905/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com