且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

自动全屏html5视频播放?

更新时间:2023-12-06 13:33:58

这应该有效:

  1. 创建一个带有 1 个带有自动播放属性的视频标签的空 html 页面

  1. create an empty html page with 1 video tag with autoplay attribute

然后使用 JavaScript 创建一个包含您要播放的视频的所有路径的数组

then with JavaScript create an array of all the paths to the videos that you want to play

将函数附加到视频标签的结束"事件

attach a function to the 'ended' event of the video tag

在函数内部生成一个随机索引Math.floor(Math.random() * clipPaths.length);

inside the function generate a random index Math.floor(Math.random() * clipPaths.length);

使用生成的索引从数组中获取随机路径

with the generated index get a random path from the array

设置路径为视频标签的'src'属性

set the path as the 'src' attribute of the video tag

你只需要把所有的路径放在clipPaths数组中...

You just need to put all the paths in the clipPaths array...

 <!DOCTYPE html>
<html>
<head>
  <style>
  video#myVideo { 
    position: fixed;
    top: 50%;
    left: 50%;
    min-width: 100%;
    min-height: 100%;
    width: auto;
    height: auto;
    z-index: -100;
    -ms-transform: translateX(-50%) translateY(-50%);
    -moz-transform: translateX(-50%) translateY(-50%);
    -webkit-transform: translateX(-50%) translateY(-50%);
    transform: translateX(-50%) translateY(-50%);

    background-size: cover; 
}
  </style>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width">
  <title>JS Bin</title>
</head>
<body>

<video id="myVideo" controls autoplay>

</video>
  <script>
    clipPaths = [
      'https://archive.org/download/WebmVp8Vorbis/webmvp8.ogv',
      'http://techslides.com/demos/sample-videos/small.mp4',
      'http://www.sample-videos.com/video/mp4/240/big_buck_bunny_240p_1mb.mp4'

    ];
    var myVideo = document.getElementById('myVideo');
    myVideo.addEventListener('ended',myHandler,false);

    function myHandler(e) {
      var randClip = clipPaths[Math.floor(Math.random() * clipPaths.length)];
      myVideo.setAttribute('src',randClip);
    }
    var randClip = clipPaths[Math.floor(Math.random() * clipPaths.length)];

    myVideo.setAttribute('src',randClip);


  </script>
  </body>
</html>

链接到工作示例 link