更新:我把这个答案留在这里作为一个例子,如何使用鼠标事件在桌面(而不是移动)浏览器中使用范围/滑动条交互。然而,我现在在本页的其他地方写了一个完全不同的,我相信更好的答案,它使用不同的方法为这个问题提供一个跨浏览器的桌面和移动解决方案。
最初的回答:
概述:一个跨浏览器的纯JavaScript(即no-jQuery)解决方案,允许读取范围输入值,而无需使用on('input'…和/或在(“变革”的…在浏览器之间工作不一致。
截至今天(2016年2月下旬),浏览器仍然不一致,所以我在这里提供了一个新的解决方案。
问题:当使用范围输入时,即滑动条,on('input'…在Mac和Windows Firefox, Chrome和Opera以及Mac Safari中提供持续更新的范围值,而在('change'…仅在鼠标向上时报告范围值。相比之下,在Internet Explorer (v11)中,on('input'…根本不起作用,并且在('change'…不断更新。
我在这里报告两种策略,使用普通JavaScript(即没有jQuery),通过使用mousedown, mouemove和(可能的)mouseup事件,在所有浏览器中获得相同的连续范围值报告。
策略1:时间短但效率低
如果你喜欢更短的代码而不是更高效的代码,你可以使用第一种解决方案,它使用鼠标向下和鼠标移动,但不使用鼠标向上。这将根据需要读取滑块,但在任何鼠标悬停事件期间继续不必要地触发滑块,即使当用户没有单击并因此没有拖动滑块时也是如此。它本质上是在'mousedown'和'mousemove'事件之后读取范围值,使用requestAnimationFrame稍微延迟每个事件。
var rng = document.querySelector("input");
读(“mousedown”);
读(“mousemove”);
读(“keydown”);//包含这个也允许键盘控制
函数读取(evtType) {
rng。addEventListener(evtType, function() {
窗口。requestAnimationFrame(function () {
document.querySelector (" div ")。innerHTML = rng.value;
rng。setAttribute(“aria-valuenow rng.value);//包含可访问性
});
});
}
50 < div > < / div > < input type = "范围" / >
策略2:时间更长但效率更高
如果你需要更高效的代码,并且可以忍受更长的代码长度,那么你可以使用下面的解决方案,它使用mousedown, mouemove和mouseup。这也会根据需要读取滑块,但一旦释放鼠标按钮,就会适当地停止读取。本质上的区别是,它只在“mousedown”之后开始监听“mousemove”,而在“mouseup”之后停止监听“mousemove”。
var rng = document.querySelector("input");
var listener = function() {
window.requestAnimationFrame(function() {
document.querySelector("div").innerHTML = rng.value;
});
};
rng.addEventListener("mousedown", function() {
listener();
rng.addEventListener("mousemove", listener);
});
rng.addEventListener("mouseup", function() {
rng.removeEventListener("mousemove", listener);
});
// include the following line to maintain accessibility
// by allowing the listener to also be fired for
// appropriate keyboard events
rng.addEventListener("keydown", listener);
<div>50</div><input type="range"/>
演示:对上述解决方案的需求和实现的更全面的解释
下面的代码更全面地演示了该策略的许多方面。演示中嵌入了解释:
var select, inp, listen, unlisten, anim, show, onInp, onChg, onDn1, onDn2, onMv1, onMv2, onUp, onMvCombo1, onDnCombo1, onUpCombo2, onMvCombo2, onDnCombo2;
select = function(selctr) { return document.querySelector(selctr); };
inp = select("input");
listen = function(evtTyp, cb) { return inp. addEventListener(evtTyp, cb); };
unlisten = function(evtTyp, cb) { return inp.removeEventListener(evtTyp, cb); };
anim = function(cb) { return window.requestAnimationFrame(cb); };
show = function(id) {
return function() {
select("#" + id + " td~td~td" ).innerHTML = inp.value;
select("#" + id + " td~td~td~td").innerHTML = (Math.random() * 1e20).toString(36); // random text
};
};
onInp = show("inp" ) ;
onChg = show("chg" ) ;
onDn1 = show("mdn1") ;
onDn2 = function() {anim(show("mdn2")); };
onMv1 = show("mmv1") ;
onMv2 = function() {anim(show("mmv2")); };
onUp = show("mup" ) ;
onMvCombo1 = function() {anim(show("cmb1")); };
onDnCombo1 = function() {anim(show("cmb1")); listen("mousemove", onMvCombo1);};
onUpCombo2 = function() { unlisten("mousemove", onMvCombo2);};
onMvCombo2 = function() {anim(show("cmb2")); };
onDnCombo2 = function() {anim(show("cmb2")); listen("mousemove", onMvCombo2);};
listen("input" , onInp );
listen("change" , onChg );
listen("mousedown", onDn1 );
listen("mousedown", onDn2 );
listen("mousemove", onMv1 );
listen("mousemove", onMv2 );
listen("mouseup" , onUp );
listen("mousedown", onDnCombo1);
listen("mousedown", onDnCombo2);
listen("mouseup" , onUpCombo2);
table {border-collapse: collapse; font: 10pt Courier;}
th, td {border: solid black 1px; padding: 0 0.5em;}
input {margin: 2em;}
li {padding-bottom: 1em;}
<p>Click on 'Full page' to see the demonstration properly.</p>
<table>
<tr><th></th><th>event</th><th>range value</th><th>random update indicator</th></tr>
<tr id="inp" ><td>A</td><td>input </td><td>100</td><td>-</td></tr>
<tr id="chg" ><td>B</td><td>change </td><td>100</td><td>-</td></tr>
<tr id="mdn1"><td>C</td><td>mousedown </td><td>100</td><td>-</td></tr>
<tr id="mdn2"><td>D</td><td>mousedown using requestAnimationFrame</td><td>100</td><td>-</td></tr>
<tr id="mmv1"><td>E</td><td>mousemove </td><td>100</td><td>-</td></tr>
<tr id="mmv2"><td>F</td><td>mousemove using requestAnimationFrame</td><td>100</td><td>-</td></tr>
<tr id="mup" ><td>G</td><td>mouseup </td><td>100</td><td>-</td></tr>
<tr id="cmb1"><td>H</td><td>mousedown/move combo </td><td>100</td><td>-</td></tr>
<tr id="cmb2"><td>I</td><td>mousedown/move/up combo </td><td>100</td><td>-</td></tr>
</table>
<input type="range" min="100" max="999" value="100"/>
<ol>
<li>The 'range value' column shows the value of the 'value' attribute of the range-type input, i.e. the slider. The 'random update indicator' column shows random text as an indicator of whether events are being actively fired and handled.</li>
<li>To see browser differences between input and change event implementations, use the slider in different browsers and compare A and B.</li>
<li>To see the importance of 'requestAnimationFrame' on 'mousedown', click a new location on the slider and compare C (incorrect) and D (correct).</li>
<li>To see the importance of 'requestAnimationFrame' on 'mousemove', click and drag but do not release the slider, and compare E (often 1 pixel behind) and F (correct).</li>
<li>To see why an initial mousedown is required (i.e. to see why mousemove alone is insufficient), click and hold but do not drag the slider and compare E (incorrect), F (incorrect) and H (correct).</li>
<li>To see how the mouse event combinations can provide a work-around for continuous update of a range-type input, use the slider in any manner and note whichever of A or B continuously updates the range value in your current browser. Then, while still using the slider, note that H and I provide the same continuously updated range value readings as A or B.</li>
<li>To see how the mouseup event reduces unnecessary calculations in the work-around, use the slider in any manner and compare H and I. They both provide correct range value readings. However, then ensure the mouse is released (i.e. not clicked) and move it over the slider without clicking and notice the ongoing updates in the third table column for H but not I.</li>
</ol>