简体   繁体   English

如何实时更新滑块的值?

[英]How do I get the value of a slider to update in real-time?

I have an HTML video tag with a customized control bar and in it I want both the seek bar and volume bar to update their values in real-time as the user scrubs through the range. 我有一个带有自定义控制栏的HTML视频标签,在其中我希望搜索栏和音量栏能够在用户擦除范围时实时更新其值。 Currently the volume updates after the user adjusts the slider and not while the user is clicking-and-dragging. 目前,用户调整滑块后更新音量,而不是在用户单击并拖动时更新。

In HTML I have them set up as such: 在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>

And in my JavaScript I have them hooked up like so: 在我的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;
});

I want to do this from scratch and not use JavaScript libraries like jQuery even though I know this has already been done for that library. 我想从头开始这样做,不要使用像jQuery这样的JavaScript库,即使我知道已经为该库完成了。 Most of the solutions I've seen involve jQuery but I don't want to use it. 我见过的大多数解决方案都涉及jQuery,但我不想使用它。 This is for: reducing the dependency on jQuery, allowing for more control on my end, and primarily as a learning experience. 这是为了:减少对jQuery的依赖,允许对我的更多控制,主要是作为学习体验。

  1. Using the mousemove is a stupid workaround. 使用mousemove是一个愚蠢的解决方法。 There is an event called 'input'. 有一个叫做“输入”的事件。
  2. The change event should be dispatched as soon as the user as commited himself to a specific value (mouserelease, keyup or blur), but beware Chrome and IE10 have bad implementations of this different input/change event behavior. 一旦用户将自己转换为特定值(mouserelease,keyup或blur),就应该调度change事件,但要注意Chrome和IE10对这种不同的输入/更改事件行为有不良实现。 (see: https://code.google.com/p/chromium/issues/detail?id=155747 ) (参见: https//code.google.com/p/chromium/issues/detail?id = 155747
  3. If you want to learn JS, learn how to structure your JS and think in components. 如果你想学习JS,学习如何构建你的JS并思考组件。 This is a lot more important than using native JS vs. JQuery. 这比使用原生JS与JQuery重要得多。 For example, you are using ids. 例如,您正在使用ID。 This means you can only have one mediaplayer on one page. 这意味着您只能在一个页面上拥有一个媒体播放器。 You are omitting the third parameter of addEventListener. 您省略了addEventListener的第三个参数。 And so on.... 等等....

So the explanation how to get things done. 所以说明如何完成工作。 Depends wether you are testing in a standards compilant browser (currently only FF) or in x-browser enviroment. 取决于您是在标准编译浏览器(目前只有FF)还是在x-browser环境中进行测试。

To get the current value of an input while the user is interacting with it, simply use the input event: 要在用户与其交互时获取输入的当前值,只需使用输入事件:

range.addEventListener('input', onInput, false);

Here is a working demo for FF: http://jsfiddle.net/trixta/MfLrW/ 这是FF的工作演示: http//jsfiddle.net/trixta/MfLrW/

In case you want to get this work in Chrome and IE, you have to use the input/change and treat them like it would be only the input event. 如果你想在Chrome和IE中使用它,你必须使用输入/更改并将它们视为只是输入事件。 Then you need to compute the "change" event yourself, which isn't really simple. 然后你需要自己计算“变化”事件,这并不是很简单。 But here is a working example for you: 但这是一个适合您的工作示例:

http://jsfiddle.net/trixta/AJLSV/ http://jsfiddle.net/trixta/AJLSV/

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在拖动 slider 时实时获取范围 slider 的值? - How to get the value of a range slider in real-time while dragging the slider? 如何在Agility.js中实时获取模型更新 - How to get real-time update of model in Agility.js 如何检测输入值何时在 JavaScript 中实时变化? - How do I detect when input value changes real-time in JavaScript? 如何在Javascript中更新实时图形 - How to update Real-time graph in Javascript 如何使用现有数据以及随机生成的 id 更新我的 firebase 实时数据库中的数据 - How do I update the data in my firebase real-time database with the existing data along side a randomly generated id 在 web 页面中与 timeonsitetracker.js API 集成时,如何获取实时“timeonsite”参数? - How do I get real-time “timeonsite” parameter when integrating with timeonsitetracker.js API in web pages? 试图进行2个输入字段的实时计算更新 - trying to do calculation of 2 input fields update in real-time 如何检查基于火的实时数据库中是否存在该值,如果不存在如何添加该值? - How can i check if the value exists in the fire-base real-time database and if not how to add that value? 从数据库中获取字段的实时值(Mysql) - Get the real-time value of a field from a database (Mysql) 如何将其置于实时状态?我已经把(async:True)但它不起作用 - How do I put this on Real-Time? I already put (async: True) but it doesnt work
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM