[英]Input range fires “input” event on mouseup in Firefox
似乎在type=range
輸入元素上使用input
事件在Webkit和Firefox中的工作方式不同。 我真的不明白為什么在滑動滑塊和釋放鼠標時事件都會觸發。
我非常不希望在釋放鼠標按鈕時再次觸發事件。
有關如何使用輸入事件停止此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>
聽取change
事件而不是input
怎么樣?
根據你的例子:
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>
盡管如此,這似乎是一個瀏覽器錯誤。 請參閱 react project中發布的此問題 。
您可以做的是將此作為顯示此行為的瀏覽器中的后備。 檢測正在執行此操作的瀏覽器並為其提供此回退,從而取消額外的增量。
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.