在 HTML5 视频中突出显示播放器搜索栏

我的 HTML5 视频播放器有一个定制的搜索栏。但我需要突出显示搜索栏的一些预定义部分,例如秒2-57-8。我怎样才能做到这一点?

基本上,我需要它是这样的:

https://img3.mukewang.com/65080076000110f106290352.jpg

到目前为止,这是我的简单代码:


<!DOCTYPE html> 

<html> 

<head>


<style>

.body{

background-color:black;

}

.video-player {

  position: relative;

  width: 66%;

  height: 66%;

}

.video-player img {

  width: 100%;

  height: 100%;

}

.video-player video {

  position: fixed;

  top: 0;

  left: 0;

  min-width: 66%;

  min-height: 66%;

  width: auto;

  height: auto;

  z-index: -100;

  background-repeat: no-repeat;

}

.video-player .controls {

  position: absolute;

  width: 100%;

  height: 100%;

  top: 0;

  left: 0;

}

.video-player .controls .progress-bar {

  position: absolute;

  margin-left: 28%;

  bottom: 10%;

  color: orange;

  font-size: 12px;

  width: 40%;

  height: 8%;

  border: none;

  background: #434343;

  border-radius: 9px;

  vertical-align: middle;

  cursor: pointer;

}

.video-player .controls progress::-moz-progress-bar {

  color: orange;

  background: #434343;

}

.video-player .controls progress[value]::-webkit-progress-bar {

  background-color: #434343;

  border-radius: 2px;

  box-shadow: 0 2px 5px rgba(0, 0, 0, 0.25) inset;

}

.video-player .controls progress[value]::-webkit-progress-value {

  background-color: orange;

}


video#backgroundvid {

  position: absolute;

  right: 0;

  bottom: 0;

  min-width: 100%;

  min-height: 100%;

  width: auto;

  height: auto;

  z-index: -100;

  background-repeat: no-repeat;


}

</style>

</head>


<body>

<div class="video-player">

  <video preload="auto" autoplay loop id="backgroundvid">

    <source src="mov_bbb.mp4" type="video/mp4">

    Your browser does not support HTML5 video.

  </video>

  <img src="top2.png" style="object-fit:cover" alt="" id="backgroundvid">

  <div class="controls">

    <progress class="progress-bar" style="object-fit:cover; z-index=10000" min="0" max="100" value="0">0% played</progress>

  </div>

</div>


肥皂起泡泡
浏览 62回答 1
1回答

慕仙森

您可以使用将叠加在进度栏顶部的画布,然后您只需在画布上绘制标记即可。只需对 html 进行轻微更改(将 id 添加到进度条id="progress-bar"):<progress id="progress-bar" class="progress-bar" style="object-fit:cover; z-index=10000" min="0" max="100" value="0">0% played</progress>添加 CSS 来设置画布的样式(与进度条具有相同的 CSS 属性)#markers{&nbsp; &nbsp; position: absolute;&nbsp; &nbsp; bottom: 10%;&nbsp; &nbsp; margin-left: 28%;&nbsp; &nbsp; border-radius: 9px;&nbsp; &nbsp; pointer-events: none;}请注意,pointer-events: none;如果不放置它,您将无法访问进度栏的控制。因此,JavaScript 创建并插入画布,然后在其上放置标记。// We need the metadata 'duration', so we wrap the code in an event listener to be sure we execute our code when the metadata is loadedvideo.addEventListener('loadedmetadata', function () {&nbsp; &nbsp; // Get the dimension of the progress-bar&nbsp; &nbsp; const progressbar = document.getElementById('progress-bar');&nbsp; &nbsp; const widthProgressBar = window.getComputedStyle(progressbar, null).getPropertyValue("width");&nbsp; &nbsp; const heightProgressBar = window.getComputedStyle(progressbar, null).getPropertyValue("height");&nbsp; &nbsp; // Create the canvas&nbsp; &nbsp; const canvas = document.createElement('canvas');&nbsp; &nbsp; const w = canvas.width = parseFloat(widthProgressBar);&nbsp; &nbsp; const h = canvas.height = parseFloat(heightProgressBar);&nbsp; &nbsp; canvas.id = 'markers';&nbsp; &nbsp; const progressBar = document.getElementById("progress-bar");&nbsp; &nbsp; // Insert the canvas in the DOM&nbsp; &nbsp; progressBar.parentNode.insertBefore(canvas, progressBar.nextSibling)&nbsp; &nbsp; // Define the context&nbsp; &nbsp; const ctx = canvas.getContext('2d');&nbsp; &nbsp; // Calcul how many px will represent 1s&nbsp; &nbsp; const videoDuration = video.duration;&nbsp; &nbsp; const ratioPxBySeconds = parseFloat(w) / videoDuration;&nbsp; &nbsp; // Define the markers&nbsp; &nbsp; const markers = {&nbsp; &nbsp; &nbsp; &nbsp; 'marker1': [2, 5],&nbsp; &nbsp; &nbsp; &nbsp; 'marker2': [7, 8]&nbsp; &nbsp; };&nbsp; &nbsp; // Function to draw the markers&nbsp; &nbsp; function setMarkers(markers, ratioPxSec, height) {&nbsp; &nbsp; &nbsp; &nbsp; for (marker in markers) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; let x = markers[marker][0] * ratioPxSec; // Start x position of the marker&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; let y = 0; // Start y position of the marker&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; let w = (markers[marker][1] - markers[marker][0]) * ratioPxSec; // Width of the marker&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; let h = parseFloat(height); // Height of the marker&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ctx.fillStyle = "#7f3302"; // Set the color of the marker&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ctx.fillRect(x, y, w, h); // Draw a rectangle&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }&nbsp; &nbsp; setMarkers(markers, ratioPxBySeconds, h); // Call the function});const player = document.querySelector('.video-player');const video = player.querySelector('video');const progressBar = player.querySelector('.progress-bar');video.addEventListener('timeupdate', updateProgressBar, false);progressBar.addEventListener('click', seek);function updateProgressBar() {&nbsp; var percentage = Math.floor((100 / video.duration) * video.currentTime);&nbsp; progressBar.value = percentage;&nbsp; progressBar.innerHTML = percentage + '% played';}function seek(e) {&nbsp; let percent = e.offsetX / this.offsetWidth;&nbsp; video.currentTime = percent * video.duration;&nbsp; e.target.value = Math.floor(percent / 100);&nbsp; e.target.innerHTML = progressBar.value + '% played';}// We need the metadata 'duration', so we wrap the code in an event listener to be sure we execute our code when the metadata is loadedvideo.addEventListener('loadedmetadata', function() {&nbsp; // Get the dimension of the progress-bar&nbsp; const progressbar = document.getElementById('progress-bar');&nbsp; const widthProgressBar = window.getComputedStyle(progressbar, null).getPropertyValue("width");&nbsp; const heightProgressBar = window.getComputedStyle(progressbar, null).getPropertyValue("height");&nbsp; // Create the canvas&nbsp; const canvas = document.createElement('canvas');&nbsp; const w = canvas.width = parseFloat(widthProgressBar);&nbsp; const h = canvas.height = parseFloat(heightProgressBar);&nbsp; canvas.id = 'markers';&nbsp; const progressBar = document.getElementById("progress-bar");&nbsp; // Insert the canvas in the DOM&nbsp; progressBar.parentNode.insertBefore(canvas, progressBar.nextSibling)&nbsp; // Define the context&nbsp; const ctx = canvas.getContext('2d');&nbsp; // Calcul how many px will represent 1s&nbsp; const videoDuration = video.duration;&nbsp; const ratioPxBySeconds = parseFloat(w) / videoDuration;&nbsp; // Define the markers&nbsp; const markers = {&nbsp; &nbsp; 'marker1': [2, 5],&nbsp; &nbsp; 'marker2': [7, 8]&nbsp; };&nbsp; // Function to draw the markers&nbsp; function setMarkers(markers, ratioPxSec, height) {&nbsp; &nbsp; for (marker in markers) {&nbsp; &nbsp; &nbsp; let x = markers[marker][0] * ratioPxSec; // Start x position of the marker&nbsp; &nbsp; &nbsp; let y = 0; // Start y position of the marker&nbsp; &nbsp; &nbsp; let w = (markers[marker][1] - markers[marker][0]) * ratioPxSec; // Width of the marker&nbsp; &nbsp; &nbsp; let h = parseFloat(height); // Height of the marker&nbsp; &nbsp; &nbsp; ctx.fillStyle = "rgb(127, 51, 2, 0.9)"; // Set the color of the marker&nbsp; &nbsp; &nbsp; ctx.fillRect(x, y, w, h); // Draw a rectangle&nbsp; &nbsp; }&nbsp; }&nbsp; setMarkers(markers, ratioPxBySeconds, h); // Call the function&nbsp;&nbsp;&nbsp; // Calculate the new dimensions & redraw&nbsp; function resize(){&nbsp; &nbsp; const progressBar = document.getElementById('progress-bar');&nbsp; &nbsp; const w = canvas.width = progressBar.clientWidth;&nbsp; &nbsp; const h = canvas.height = progressBar.clientHeight;&nbsp; &nbsp; const ratioPxBySeconds = parseFloat(w) / videoDuration;&nbsp; &nbsp; setMarkers(markers, ratioPxBySeconds, h);&nbsp; }&nbsp; // On page resize, call the resize() function&nbsp; window.addEventListener("resize", resize, false);&nbsp;&nbsp;});body {&nbsp; background-color: black;}.video-player {&nbsp; position: relative;&nbsp; width: 66%;&nbsp; height: 66%;}.video-player img {&nbsp; width: 100%;&nbsp; height: 100%;}.video-player video {&nbsp; position: fixed;&nbsp; top: 0;&nbsp; left: 0;&nbsp; min-width: 66%;&nbsp; min-height: 66%;&nbsp; width: auto;&nbsp; height: auto;&nbsp; z-index: -100;&nbsp; background-repeat: no-repeat;}.video-player .controls {&nbsp; position: absolute;&nbsp; width: 100%;&nbsp; height: 100%;&nbsp; top: 0;&nbsp; left: 0;}.video-player .controls .progress-bar {&nbsp; position: absolute;&nbsp; margin-left: 28%;&nbsp; bottom: 10%;&nbsp; color: orange;&nbsp; font-size: 12px;&nbsp; width: 40%;&nbsp; height: 8%;&nbsp; border: none;&nbsp; background: #434343;&nbsp; border-radius: 9px;&nbsp; vertical-align: middle;&nbsp; cursor: pointer;}#markers {&nbsp; position: absolute;&nbsp; bottom: 10%;&nbsp; margin-left: 28%;&nbsp; border-radius: 9px;&nbsp; pointer-events: none;}.video-player .controls progress::-moz-progress-bar {&nbsp; color: orange;&nbsp; background: #434343;}.video-player .controls progress[value]::-webkit-progress-bar {&nbsp; background-color: #434343;&nbsp; border-radius: 2px;&nbsp; box-shadow: 0 2px 5px rgba(0, 0, 0, 0.25) inset;}.video-player .controls progress[value]::-webkit-progress-value {&nbsp; background-color: orange;}video#backgroundvid {&nbsp; position: absolute;&nbsp; right: 0;&nbsp; bottom: 0;&nbsp; min-width: 100%;&nbsp; min-height: 100%;&nbsp; width: auto;&nbsp; height: auto;&nbsp; z-index: -100;&nbsp; background-repeat: no-repeat;}<div class="video-player">&nbsp; <video preload="auto" autoplay loop id="backgroundvid">&nbsp; &nbsp; <source src="https://www.w3schools.com/html/mov_bbb.mp4" type="video/mp4">&nbsp; &nbsp; Your browser does not support HTML5 video.&nbsp; </video>&nbsp; <img src="https://i.stack.imgur.com/gmK7P.png" style="object-fit:cover" alt="" id="backgroundvid">&nbsp; <div class="controls">&nbsp; &nbsp; <progress id="progress-bar" class="progress-bar" style="object-fit:cover; z-index=10000" min="0" max="100" value="0">0% played</progress>&nbsp; </div></div>编辑:添加了 resize() 函数,用于在屏幕调整大小时更新标记(通常,当您将视频全屏播放时会发生这种情况)// Calculate the new dimensions & redrawfunction resize(){&nbsp; const progressBar = document.getElementById('progress-bar');&nbsp; const w = canvas.width = progressBar.clientWidth;&nbsp; const h = canvas.height = progressBar.clientHeight;&nbsp; const ratioPxBySeconds = parseFloat(w) / videoDuration;&nbsp; setMarkers(markers, ratioPxBySeconds, h);}// On page resize, call the resize() functionwindow.addEventListener("resize", resize, false);
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Html5