【发布时间】:2021-03-11 03:34:54
【问题描述】:
我正在尝试使用 HTML5 创建一个视频播放器,当点击播放时会全屏显示。
我找到了一个非常优雅的解决方案:
document.addEventListener('click', function (event) {
// Check if clicked element is a video thumbnail
var videoId = event.target.getAttribute('data-video');
if (!videoId) return;
// Create iframe
var iframe = document.createElement('div');
iframe.innerHTML = '<p>x</p><iframe width="165" height="146" src="https://www.youtube.com/embed/' + videoId + '?rel=0&autoplay=1" frameborder="0" allow="autoplay; encrypted-media" allowfullscreen></iframe>';
var video = iframe.childNodes[1];
// Replace the image with the video
event.target.parentNode.replaceChild(video, event.target);
// Enter fullscreen mode
video.requestFullscreen();
}, false);
在哪里
<div class="wrapper" style="margin-bottom: 20px;border: 1px solid red;margin-right: 11px;">
<p><img data-video="example" alt="Play this video" src="/video/example.jpg" style="width: 164px; height: 114px;"></p>
</div>
现在,当我尝试在其上添加播放按钮时,我做到了:
<div class="wrapper" style="margin-bottom: 20px;border: 1px solid red;margin-right: 11px;">
<p><img src="/video/example.jpg" style="width: 164px; height: 114px;"></p>
<div class="playpause" id="playButton" data-video="example" alt="Play this video"></div>
</div>
所以它从播放按钮中获取数据。但是现在,视频缩略图不会被视频替换,而是在下面用 iframe 创建另一个。
如何在点击时移除播放按钮并替换图片?
【问题讨论】:
标签: javascript html html5-video html5-fullscreen