[英]Input range fires “input” event on mouseup in Firefox
It seems like using the input
event on a type=range
input element works differently in Webkit and Firefox. 似乎在
type=range
输入元素上使用input
事件在Webkit和Firefox中的工作方式不同。 I really don't understand why the event fires both on sliding the slider and while releasing the mouse. 我真的不明白为什么在滑动滑块和释放鼠标时事件都会触发。
I very much do not want the event to fire again when releasing the mouse button. 我非常不希望在释放鼠标按钮时再次触发事件。
Any ideas how to stop this mouseup
nonsense with the input event? 有关如何使用输入事件停止此
mouseup
废话的任何想法?
var counter = document.querySelector('div'), rangeInput = document.querySelector('input'); rangeInput.addEventListener('input', function(){ counter.innerHTML = 1 + +counter.innerHTML; });
body{ font:18px Arial; } input{ width:80%; } div{ color:red; } div::before{ content:'Event fired: '; color:#555; } div::after{ content:' times'; color:#555; }
<p>Try to click the slider, wait and release the mouse</p> <input type="range" min="1" max="100" step="1"> <div>0</div>
How about listening to the change
event instead of input
? 听取
change
事件而不是input
怎么样?
As per your example: 根据你的例子:
var counter = document.querySelector('div'), rangeInput = document.querySelector('input'); rangeInput.addEventListener('change', function(){ counter.innerHTML = 1 + +counter.innerHTML; });
body{ font:18px Arial; } input{ width:80%; } div{ color:red; } div::before{ content:'Event fired: '; color:#555; } div::after{ content:' times'; color:#555; }
<p>Try to click the slider, wait and release the mouse</p> <input type="range" min="1" max="100" step="1"> <div>0</div>
This seems to be a browser bug nevertheless. 尽管如此,这似乎是一个浏览器错误。 See this issue posted in react project.
请参阅 react project中发布的此问题 。
What you could do is to have this as a fallback in browsers that show this behaviour. 您可以做的是将此作为显示此行为的浏览器中的后备。 Detect the browsers that are doing this and have this fallback for them which cancels out the extra increament.
检测正在执行此操作的浏览器并为其提供此回退,从而取消额外的增量。
var counter = document.querySelector('div'), rangeInput = document.querySelector('input'); rangeInput.addEventListener('input', function(){ counter.innerHTML = 1 + +counter.innerHTML; }); rangeInput.addEventListener('change', function(){ /* if firefox - http://stackoverflow.com/a/9851769/1437261 */ if(typeof InstallTrigger !== 'undefined'){ counter.innerHTML = counter.innerHTML - 1; } return false; });
body{ font:18px Arial; } input{ width:80%; } div{ color:red; } div::before{ content:'Event fired: '; color:#555; } div::after{ content:' times'; color:#555; }
<p>Try to click the slider, wait and release the mouse</p> <input type="range" min="1" max="100" step="1"> <div>0</div>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.