<video id="myVideo" src="myVideo.ogv"></video> <input type="button" value="播放/暂停"/></br> <script> var myVideo =document.querySelector("#myVideo"); var oBtn =document.querySelector("input") oBtn.onclick = function (){ if(myVideo.paused) myVideo.play(); else myVideo.pause(); } </script> 为上述代码增添一些新功能,并给出添加后的代码
时间: 2023-06-24 17:05:41 浏览: 124
vue使用video.js进行视频播放功能
以下是为上述代码增添新功能后的代码:
```
<video id="myVideo" src="myVideo.ogv"></video>
<input type="button" value="播放/暂停"/>
<input type="range" id="volumeBar" min="0" max="1" step="0.1" value="1"/>
<input type="range" id="seekBar" min="0" step="0.1"/>
<span id="currentTime">00:00</span>/<span id="duration">00:00</span>
<script>
var myVideo = document.querySelector("#myVideo");
var playBtn = document.querySelector("input[value='播放/暂停']");
var volumeBar = document.querySelector("#volumeBar");
var seekBar = document.querySelector("#seekBar");
var currentTime = document.querySelector("#currentTime");
var duration = document.querySelector("#duration");
// 播放/暂停视频
playBtn.onclick = function () {
if (myVideo.paused) {
myVideo.play();
this.value = "暂停";
} else {
myVideo.pause();
this.value = "播放";
}
};
// 调节音量
volumeBar.oninput = function () {
myVideo.volume = this.value;
};
// 调节进度条
seekBar.oninput = function () {
myVideo.currentTime = this.value;
};
// 更新视频当前时间和总时长
myVideo.ontimeupdate = function () {
var curTime = formatTime(myVideo.currentTime);
var totalDuration = formatTime(myVideo.duration);
currentTime.textContent = curTime;
duration.textContent = totalDuration;
seekBar.value = myVideo.currentTime;
};
// 格式化时间
function formatTime(time) {
var minutes = Math.floor(time / 60);
var seconds = Math.floor(time % 60);
return (minutes < 10 ? "0" + minutes : minutes) + ":" + (seconds < 10 ? "0" + seconds : seconds);
}
</script>
```
代码中新增了调节音量、调节进度条、展示当前时间和总时长等功能,同时对代码进行了一些细节上的优化,比如添加了格式化时间函数,让时间显示更加友好。
阅读全文