简体   繁体   English

如何判断输入范围元素在拖动时是增加还是减少

[英]how to tell if an input range element is increasing or decreasing while dragging

So I have some <input type="range"> elements that I need to know if the value is increasing or decreasing while the user is dragging the range slider. 因此,我有一些<input type="range">元素,我需要知道在用户拖动范围滑块时值是在增加还是在减少。 So far all I've been able to find/implement are things that use when a user first touches or releases the slider and compare the current changing value to that last number, but that won't work because it can't compensate for when the user is still dragging. 到目前为止,我所能找到/实现的只是在用户第一次触摸或释放滑块并将当前的更改值与最后一个数字进行比较时使用的东西,但这是行不通的,因为它无法补偿当用户仍在拖动。

Is there a way to achieve this using just JS math? 有没有一种方法可以仅使用JS数学来实现?

I found out from this post that it's not what it seems in regards to capturing the onInput or onChange events. 我从这篇文章中发现,与捕获onInputonChange事件onInput To capture every movement of the slider use onInput . 要捕获滑块的每个运动,请使用onInput Below is the differences illustrated. 下面是说明的差异。

You can do your same logic as you thought, of saving the last value, then checking if the current is less or greater then the last to see if it's increasing or decreasing. 您可以按照自己的想法进行逻辑操作,保存最后一个值,然后检查电流是否小于或大于最后一个,以查看电流是增加还是减少。

EDIT: Notice that if you use the arrow keys to move the slider, both events get triggered at the same time. 编辑:请注意,如果使用箭头键移动滑块,则两个事件将同时触发。 If you use the mouse, both will get triggered only when you stop moving, and let go of the mouse button. 如果使用鼠标,则只有当您停止移动并释放鼠标按钮时,两者才会被触发。

 var lastNum = 0; function onInput(value) { if(lastNum < value) { console.log("increasing"); } else { console.log("decreasing"); } lastNum = value; console.log("onInput: " + value); } function onChange(value) { console.log("onChange: " + value); } 
 <input type="range" oninput="onInput(this.value)" onchange="onChange(this.value)"> 

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM