Ush*_*nro 6 html javascript html5 volume slider
我有一个带有自定义控制栏的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>
Run Code Online (Sandbox Code Playgroud)
在我的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;
});
Run Code Online (Sandbox Code Playgroud)
我想从头开始这样做,不要使用像jQuery这样的JavaScript库,即使我知道已经为该库完成了.我见过的大多数解决方案都涉及jQuery,但我不想使用它.这是为了:减少对jQuery的依赖,允许对我的更多控制,主要是作为学习体验.
ale*_*kas 15
所以说明如何完成工作.取决于您是在标准编译浏览器(目前只有FF)还是在x-browser环境中进行测试.
要在用户与其交互时获取输入的当前值,只需使用输入事件:
range.addEventListener('input', onInput, false);
Run Code Online (Sandbox Code Playgroud)
这是FF的工作演示:http://jsfiddle.net/trixta/MfLrW/
如果你想在Chrome和IE中使用它,你必须使用输入/更改并将它们视为只是输入事件.然后你需要自己计算"变化"事件,这并不是很简单.但这是一个适合您的工作示例:
http://jsfiddle.net/trixta/AJLSV/
归档时间: |
|
查看次数: |
11344 次 |
最近记录: |