如何在视频上添加视频控件

How to add video controls over the video

提问人:Eliseo 提问时间:11/6/2023 更新时间:11/18/2023 访问量:60

问:

我有一个带有自定义控件的视频。

我需要随时使控件可用,但是当我打开视频的全屏版本时,控件会隐藏。

这是我的代码:

var video = document.querySelector(".video");
var toggleButton = document.querySelector(".toggleButton");
var toggleButtonId = document.getElementById("toggleButton");
var progress = document.querySelector(".progress");
var progressBar = document.querySelector(".progress__filled");
var progressId = document.getElementById('progressId');
var volumeBar = document.getElementById('volume-bar');
function togglePlay() {
 if (video.paused || video.ended) {
    video.play();
  } else {
    video.pause();
 }
}
function updateToggleButton() {
  toggleButtonId.innerHTML = video.paused ? "►" : "❚❚";
}

function handleProgress() {
  const progressPercentage = (video.currentTime / video.duration) * 100;
  progressBar.style.flexBasis = `${progressPercentage}%`;
}

function scrub(e) {
  const scrubTime = (e.offsetX / progress.offsetWidth) * video.duration;
  video.currentTime = scrubTime;
}
volumeBar.addEventListener("change", function(evt) {
    video.volume = evt.target.value;
});
video.addEventListener("click", togglePlay);
video.addEventListener("play", updateToggleButton);
video.addEventListener("pause", updateToggleButton);

video.addEventListener("timeupdate", handleProgress);
progressId.addEventListener("click", scrub);
var mousedown = false;
progressId.addEventListener("mousedown", () => (mousedown = true));
progressId.addEventListener("mousemove", (e) => mousedown && scrub(e));
progressId.addEventListener("mouseup", () => (mousedown = false));
function toggleFullScreen() {
  if (video.requestFullscreen) {
      if (document.fullScreenElement) {
          document.cancelFullScreen();
      } else {
          video.requestFullscreen();
      }
  }
  else if (video.msRequestFullscreen) {
      if (document.msFullscreenElement) {
          document.msExitFullscreen();
      } else {
          video.msRequestFullscreen();
      }
  }
  else if (video.mozRequestFullScreen) {
      if (document.mozFullScreenElement) {
          document.mozCancelFullScreen();
      } else {
          video.mozRequestFullScreen();
      }
  }
  else if (video.webkitRequestFullscreen) {
      if (document.webkitFullscreenElement) {
          document.webkitCancelFullScreen();
      } else {
          video.webkitRequestFullscreen();
      }
  }
  else {
      alert("Pantalla completa no esta soportada");
  }
}
<html data-bs-theme="dark" >
  <head>
  <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/[email protected]/font/bootstrap-icons.css">
  <link href="https://cdn.jsdelivr.net/npm/[email protected]/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-T3c6CoIi6uLrA9TneNEoa7RxnatzjcDSCmG1MXxSR1GAsXEV/Dwwykc2MPK8M2HN" crossorigin="anonymous">
<script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/js/bootstrap.bundle.min.js" integrity="sha384-C6RzsynM9kWDrMNeT87bh95OGNyZPhcTNXj1NW7RuBCsyN/o0jlpcV8Qyq46cDfL" crossorigin="anonymous"></script>

  </head>
  <div>
<video id="video" class="video w-100">
<source src="https://test.loro.ec/www/t/test.loro.ec/images/articles_blocks/608.mp4" type="video/mp4">
</video>
  <div class="controls position-relative" style="top: -50px;margin-left: 8px;">
  <div class="row">
  <div class="col-1">
      <button class="controls__button toggleButton btn" id="toggleButton" title="Toggle Play" onclick="togglePlay()">►</button>
      </div>
      <div class="progress col-5 p-0" id="progressId">
      <div class="progress__filled" style="background: rebeccapurple;"></div>
    </div>
    <div class="col-4">
     <i class="bi bi-volume-up"></i> 
     <input class="form-range" style="width: 80%;" type="range" id="volume-bar" title="volume" min="0" max="1" step="0.1" value="1">
     </div>
  <div class="col-1">
    <button class="btn" onclick="toggleFullScreen()"><i class="bi bi-arrows-fullscreen"></i></button>
    </div>
    </div>
  </div></div>
</html>

代码笔:https://codepen.io/alexvambato/pen/QWYGeGV

我已经实现了外部库(bootstrap)

我需要任何答案而不使用jquery

javascript html css 视频 html5-video

评论


答:

0赞 Brett Donald 11/6/2023 #1

您不能在全屏模式下拥有自己的控件。全屏视频由操作系统控制,而不是由 Web 浏览器控制。如果您需要保留自己的控件,请不要提供全屏选项。相反,您可以提供一个“整页”选项,该选项在网页上方以全角叠加显示您的视频。

评论

0赞 Eliseo 11/7/2023
好的,但是 youtube 有一个带控件的全屏选项。你知道在 youtube 中如何做吗?
0赞 Eliseo 11/7/2023
可能的解决方案是创建一个脚本来全屏执行所有网页,然后在此功能中创建整页视频
1赞 VC.One 11/18/2023 #2

您需要将视频播放器内容(图标、视频对象等)放入一个,
然后全屏显示该特定的 div 元素。另外,这个其他答案可能会对你有所帮助。
<div>

解决方案:
在下面的示例代码中,将创建一个 id 为 的新 div,以包含该标签及其相关的 UI 元素。
container_vid<video>

(1) 更新您的 JavaScript 代码:

在第一行(about etc...)中,更新它以访问“container”div 作为变量。var video

var container_vid = document.getElementById("vid_container");
var video = document.querySelector(".video");
//etc... etc... rest of JS code

然后将函数更新为:toggleFullScreen()

function toggleFullScreen() 
{
  if (container_vid.requestFullscreen) 
  {
      if (document.fullScreenElement) { document.cancelFullScreen(); } 
      else { container_vid.requestFullscreen(); }
  }
  else if (container_vid.msRequestFullscreen) 
  {
      if (document.msFullscreenElement) { document.msExitFullscreen(); } 
      else { container_vid.msRequestFullscreen(); }
  }
  else if (container_vid.mozRequestFullScreen) 
  {
      if (document.mozFullScreenElement) { document.mozCancelFullScreen(); } 
      else { container_vid.mozRequestFullScreen(); }
  }
  else if (container_vid.webkitRequestFullscreen) 
  {
      if (document.webkitFullscreenElement) { document.webkitCancelFullScreen(); } 
      else { container_vid.webkitRequestFullscreen(); }
  }
  else { alert("Pantalla completa no esta soportada"); }
}

(2) 更新 HTML 代码以包含容器:<div>

<html data-bs-theme="dark" >
<head>
</head>
<div>

<div id="vid_container" > <!--open new div for containing video UI -->

<video id="video" class="video w-100">
<source src="https://test.loro.ec/www/t/test.loro.ec/images/articles_blocks/608.mp4" type="video/mp4">
</video>
<div class="controls position-relative" style="top: -50px;margin-left: 8px;">
<div class="row">
<div class="col-1">
  <button class="controls__button toggleButton btn" id="toggleButton" title="Toggle Play" onclick="togglePlay()">►</button>
  </div>
  <div class="progress col-5 p-0" id="progressId">
  <div class="progress__filled" style="background: rebeccapurple;"></div>
</div>
<div class="col-4">
 <i class="bi bi-volume-up"></i> 
 <input class="form-range" style="width: 80%;" type="range" id="volume-bar" title="volume" min="0" max="1" step="0.1" value="1">
 </div>
<div class="col-1">
<button class="btn" onclick="toggleFullScreen()"><i class="bi bi-arrows-fullscreen"></i></button>
</div>
</div>
</div>

</div>  <!-- close div for containing video UI -->

</div>
</html>