jquery - HTML5 视频在滚动条上显示自定义计时

标签 jquery html5-video

当我滚动时,有没有办法显示自定义计时,如下面的我的屏幕截图。

  1. 示例,由于这是 33 秒的视频,滚动时它应显示滚动下方的计时 00:03

  2. 下面是我的 html5 代码

enter image description here

<!DOCTYPE html> 
  
<html lang="en"> 
<head> 
   <meta charset="utf-8"> 
   <title>Custom HTML5 Video Player</title> 
</head> 
<body> 
  
<div id="container"> 
  
    <h1> HTML5 Video </h1> 
  
    <div id="video_container"> 
        <video controls="true"> 
            <source src="http://nettuts.s3.amazonaws.com/763_sammyJSIntro/trailer_test.mp4" type="video/mp4"> 
            <source src="http://nettuts.s3.amazonaws.com/763_sammyJSIntro/trailer_test.ogg" type="video/ogg"> 
              
            <!-- LINK TO FLASH FALLBACK PLAYER HERE. COULD BE EMBED OR OBJECT ELEMENT -->
        </video> 
    </div> 
  
</div>     
  
</body> 
</html>

enter image description here

最佳答案

var video = document.getElementById("Video1");

function seek() {
  video.addEventListener("timeupdate", function() {
    //  Current time  
    var vTime = video.currentTime;
    var left_begin = 105; //position where you want the displayed text to begin
    var left_end = 180; // position where it is at the end
    var width = left_end - left_begin;

    $("#curTime").css({
      left:left_begin + (width * (vTime / video.duration)) + "px"
    });
    console.log( $("#curTime").offset().left);

    document.getElementById("curTime").textContent = vTime.toFixed(1);
  }, false);


}
#curTime {
  left: 100px;
  position: absolute;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

  <video id="Video1" width="320" height="240" controls onseeking="seek()">
  <source src="https://www.w3schools.com/tags/mov_bbb.mp4" type="video/mp4">
  <source src="https://www.w3schools.com/tags/mov_bbb.ogg" type="video/ogg">
</video>
  <div id="curTime"></div>

看起来像 Microsoft为您提供答案。向下滚动到“我在哪里”。那里有一个小的 javascript 函数可以获取当前时间。然后,您可以使用返回的值创建您自己的元素,该元素将出现在视频下方并滑动。

对于seeking事件,可以使用onseeking函数,如w3schools tutorial

希望有帮助。

关于jquery - HTML5 视频在滚动条上显示自定义计时,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48666833/

相关文章:

php - 使用 php、mysql 和 jQuery 实现自动完成

jQuery.validate.js 和 asp.net 母版页

javascript - 哪些字节的视频数据是指当前播放位置

javascript - 如何使 Javascript 消息变成超链接?

javascript - 使用ajax保存时选择自动完成列表不会完整保存单词

javascript修改

php - 仅从 html5 视频/音频加载元数据

HTML 视频标签在 Google Chrome 中将 MOV 文件读取为音频文件

cordova - 为什么使用 Ionic cordovaCapture(Apache Cordova 媒体捕获插件)在 iOS 上录制低分辨率视频 (480x360)?

html - 视频无法在浏览器中播放