检查我的解决方案。您可以根据自己的情况找出样式,我span
在视频上方使用 as 叠加层,当您单击它时,它会消失并播放视频。
const videoSection = document.querySelector('.video');
if(videoSection) {
const playBtn = document.querySelector('.play-btn');
playBtn.addEventListener('click', playVideo);
}
function playVideo() {
const iframeHolder = document.querySelector('.iframe-holder');
const iframeEl = document.querySelector('.iframe-holder iframe');
const playStr = '?autoplay=true';
let baseUrl = iframeEl.getAttribute('src');
iframeHolder.classList.add('play');
iframeEl.setAttribute('src', `${baseUrl}${playStr}`);
iframeEl.setAttribute('allow', 'autoplay');
}
<!doctype html>
<html class="no-js" lang="">
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1">
<meta name="description" content="">
<title>Title</title>
</head>
<body>
<div class="video">
<span class='play-btn'></span>
<iframe allowfullscreen="" frameborder="0" src="https://player.youku.com/embed/XMjUzMzY2ODQ4OA=="></iframe>
</div>
</body>
</html>
const videoSection = document.querySelector('.video');
if(videoSection) {
const playBtn = document.querySelector('.play-btn');
playBtn.addEventListener('click', playVideo);
}
function playVideo() {
const iframeHolder = document.querySelector('.iframe-holder');
const iframeEl = document.querySelector('.iframe-holder iframe');
const playStr = '?autoplay=true';
let baseUrl = iframeEl.getAttribute('src');
iframeHolder.classList.add('play');
iframeEl.setAttribute('src', `${baseUrl}${playStr}`);
iframeEl.setAttribute('allow', 'autoplay');
}
<!doctype html>
<html class="no-js" lang="">
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1">
<meta name="description" content="">
<title>Title</title>
</head>
<body>
<div class="video">
<span class='play-btn'></span>
<iframe allowfullscreen="" frameborder="0" src="https://player.youku.com/embed/XMjUzMzY2ODQ4OA=="></iframe>
</div>
</body>
</html>