小码哥的IT人生

HTML Audio/Video DOM currentTime 属性 详解

HTML基础 2022-06-02 15:33:06小码哥的IT人生shichen

HTML Audio/Video DOM currentTime 属性

实例

将时间位置设置为 5 秒:

myVid=document.getElementById("video1");
myVid.currentTime=5;

 

完整实例【亲自试一试】:

<!DOCTYPE html>
<html>
<body>
<button onclick="getCurTime()" type="button">获得当前时间的位置</button>
<button onclick="setCurTime()" type="button">把时间位置设置为 5 秒</button>
<br />
<br />
<video id="video1" controls="controls">
  <source src="/demo/example/html5/mov_bbb.mp4" type="video/mp4">
  <source src="/demo/example/html5/mov_bbb.ogg" type="video/ogg">
  Your browser does not support HTML5 video.
</video>
<script>
myVid=document.getElementById("video1");
function getCurTime()
  {
  alert(myVid.currentTime);
  }
function setCurTime()
  {
  myVid.currentTime=5;
  }
</script>
</body>
</html>

可以使用本站在线JavaScript测试工具测试上述代码运行效果:http://www.phpcodeweb.com/runjs.html

定义和用法

currentTime 属性设置或返回音频/视频播放的当前位置(以秒计)。

当设置该属性时,播放会跳跃到指定的位置。

浏览器支持

所有主流浏览器都支持 currentTime 属性。

注释:Internet Explorer 8 或更早的浏览器不支持该属性。

语法

设置 currentTime 属性:

audio|video.currentTime="seconds"

返回 currentTime 属性:

audio|video.currentTime

属性值

描述
seconds 指示音频/视频播放的当前位置(以秒计)。

技术细节

返回值 数字值,表示秒

版权所有 © 小码哥的IT人生
Copyright © phpcodeweb All Rights Reserved
ICP备案号:苏ICP备17019232号-2  

苏公网安备 32030202000762号

© 2021-2024