我有一个带有自定义控制条的HTML视频标签,我希望在用户拖动范围时,即时更新进度条和音量条的值。目前,音量条只会在用户调整滑块后更新而不是用户点击和拖动时更新。
在HTML中,我将它们设置为:
在HTML中,我将它们设置为:
<div id="video-controls">
// ...
<input type="range" id="seek-bar" value="0">
<input type="range" id="volume-bar" min="0" max="1" step="0.01" value="1">
// ...
</div>
在我的JavaScript中,我将它们连接起来的方式如下:
// Change current viewing time when scrubbing through the progress bar
seekBar.addEventListener('change', function() {
// Calculate the new time
var time = video.duration * (seekBar.value / 100);
// Update the video time
video.currentTime = time;
});
// Update the seek bar as the video plays
video.addEventListener('timeupdate', function() {
// Calculate the slider value
var value = (100 / video.duration) * video.currentTime;
// Update the slider value
seekBar.value = value;
});
// Pause the video when the seek handle is being dragged
seekBar.addEventListener('mousedown', function() {
video.pause();
});
// Play the video when the seek handle is dropped
seekBar.addEventListener('mouseup', function() {
video.play();
});
// Adjust video volume when scrubbing through the volume bar
volumeBar.addEventListener('change', function() {
// Update the video volume
video.volume = volumeBar.value;
});
我希望从头开始做这件事,而不使用像jQuery这样的JavaScript库,尽管我知道已经有人为那个库完成了这个任务。大多数解决方案都涉及到jQuery,但我不想使用它。这是为了:减少对jQuery的依赖,允许更多的控制权,以及主要作为学习经验。
mousemove
吗? - SparoHawk