我跟着这个: Fire an event on play of youtube iframe embed 但在我的情况下,我隐藏/显示播放/暂停的div。以下工作但它会在第一次播放点击时触发两个事件,仅在第一次播放时触发。就像是: 首播点击 - > slideUp - > slideDown - >效果基本show 之后它可以正常工作在播放 - >滑动和在停止 - > slideDown
任何人都可以看到我错在哪里吗?
<script src="https://www.youtube.com/iframe_api"></script>
<div class="header_div">HEADER DIV</div>
<div id="video"></div>
var player, playing = false;
function onYouTubeIframeAPIReady() {
player = new YT.Player('video', {
height: '450',
width: '800',
videoId: 'jNQXAC9IVRw',
events: {
'onStateChange': onPlayerStateChange
}
});
}
function onPlayerStateChange(event) {
if(!playing){
$(".header_div").slideUp('slow');
playing = true;
} else {
$(".header_div").slideDown('slow');
playing = false;
}
}
答案 0 :(得分:0)
玩家有多个状态会在付费活动开始时触发向上滑动,并在玩家暂停或结束时向下滑动
function onPlayerStateChange(event) {
if(event.data == YT.PlayerState.PLAYING && !playing){
$(".header_div").slideUp('slow');
playing = true;
} else if(event.data == YT.PlayerState.PAUSED || event.data == YT.PlayerState.ENDED) {
$(".header_div").slideDown('slow');
playing = false;
}