【发布时间】:2020-12-08 13:27:15
【问题描述】:
我正在尝试在文档加载 5 秒后在弹出窗口中自动播放 youtube 视频,不幸的是,它不起作用:
我从教程中获取了这段代码,视频在点击时播放,但我真正需要的是在页面加载后自动显示视频,无需用户进行任何操作。我尝试通过添加$(document).ready 自己修改代码,但不幸的是,它没有按预期工作。
任何帮助将不胜感激 - 谢谢!
$(document).ready(function() {
setTimeout(function() {
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.
var player;
function onYouTubeIframeAPIReady() {
player = new YT.Player('player', {
height: '315',
width: '560',
videoId: 'jNQXAC9IVRw',
events: {
'onStateChange': onPlayerStateChange
}
});
}
function onPlayerStateChange(event) {
if (event.data == YT.PlayerState.PLAYING) {
//player is playing
} else {
//player is paused
}
}
function stopVideo() {
player.stopVideo();
}
function playVideo() {
player.playVideo();
}
function pauseVideo() {
player.pauseVideo();
}
$(document).on('opened.fndtn.reveal', '[data-reveal]', function() {
playVideo();
});
$(document).on('closed.fndtn.reveal', '[data-reveal]', function() {
pauseVideo();
});
}, 5000); // 5000 to load it after 5 seconds from page load
});
<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script>
</head>
<div id="myModal" class="reveal-modal" data-reveal>
<h2>Video</h2>
<div id="player"></div>
<a class="close-reveal-modal">×</a>
</div>
编辑
我已经添加了 jQuery,但是视频没有加载也没有播放,任何帮助将不胜感激。
【问题讨论】:
标签: javascript html jquery