javascript - 如何循环播放背景视频并删除控件,同时仍保留我的随机背景视频?

标签 javascript html css video background

所以我将我的视频设为我的背景,脚本会在加载或更新时刷新。

每当我启动我的网站时,我都能看到视频控件不到一秒钟,然后视频开始播放。

我怎样才能删除它并让我的视频循环播放?

此外,当您在视频结束后缩小页面时,您可以看到控件。我将在底部附上屏幕截图。

如果需要,我可以提供指向我的网站的链接,您可以在其中看到我在说什么。请告诉我。

.video-background {
  background: #000;
  position: fixed;
  top: 0; right: 0; bottom: 0; left: 0;
  overflow: hidden;
  z-index: -100;
  pointer-events: none;
}
.video-foreground,
.video-background iframe {
  position: absolute;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  pointer-events: none;
}
#vidtop-content {
	top: 0;
	color: #fff;
}
.vid-info { position: absolute; top: 0; right: 0; width: 33%; background: rgba(0,0,0,0.3); color: #fff; padding: 1rem; font-family: Avenir, Helvetica, sans-serif; }
.vid-info h1 { font-size: 2rem; font-weight: 700; margin-top: 0; line-height: 1.2; }
.vid-info a { display: block; color: #fff; text-decoration: none; background: rgba(0,0,0,0.5); transition: .6s background; border-bottom: none; margin: 1rem auto; text-align: center; }
@media (min-aspect-ratio: 16/9) {
  .video-foreground { height: 300%; top: -100%; }
}
@media (max-aspect-ratio: 16/9) {
  .video-foreground { width: 300%; left: -100%; }
}
@media all and (max-width: 600px) {
.vid-info { width: 50%; padding: .5rem; }
.vid-info h1 { margin-bottom: .2rem; }
}
@media all and (max-width: 500px) {
.vid-info .acronym { display: none; }
}
	    <script>
        var videos = ["./Videos/1.mp4", "./Videos/2.mp4", "./Videos/3.mp4", "./Videos/4.mp4"];
        window.onload = function () {
            var playerDiv = document.getElementById("random_player");
            var player = document.createElement("IFRAME");
            var randomVideoUrl = videos[Math.floor(Math.random() * videos.length)];
            player.setAttribute('width', '100%');
            player.setAttribute('height', '100%');
            player.setAttribute('src', randomVideoUrl);
            playerDiv.appendChild(player);
        };
    </script>
    
    <div class="video-background">
    <div class="video-foreground">
    <div id="random_player" />
    </div>
  </div>
	
    
    

Screenshot

最佳答案

删除控件:

player.controls = false;

循环播放:

player.addEventListener('ended', function() {
    this.currentTime = 0;
    this.play();
}, false);

player.play();

但我看到您正在将它加载到 iframe 中。这是为什么?为什么不简单地使用 video HTML 元素?它会是这样的:

window.onload = function () {
    var playerDiv = document.getElementById('random_player');
    var player = document.createElement('video');
    var sourceVideo = document.createElement('source');
    var randomVideoUrl = videos[Math.floor(Math.random() * videos.length)];
    player.setAttribute('width', '100%');
    player.setAttribute('height', '100%');
    sourceVideo.setAttribute('src', randomVideoUrl);
    playerDiv.appendChild(player);
    player.appendChild(sourceVideo);
    player.controls = false;
    player.addEventListener('ended', function () {
        this.currentTime = 0;
        this.play();
    }, false);
    player.play();
};

关于javascript - 如何循环播放背景视频并删除控件,同时仍保留我的随机背景视频?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50499906/

相关文章:

javascript - 时间表的动态页面

javascript - 将彩虹文本应用于类里面的所有文本

javascript - JavaScript 中的对象属性值

javascript - (Spring,AngularJS)我不知道为什么它是NULL

javascript - 单击时带有弹出窗口(信息窗口)的 Google map 自定义标记

CSS 问题 : Large letter is messing with my div alignments

javascript - 如何在 javascript/angular 函数中为全局变量设置值?

javascript - 处理 JavaScript 中的链接

html - 2 100% 列 + 粘性页脚

html - CSS 不适用