当我玩<input type="range">时,Firefox只有在我们将滑块拖放到Chrome和其他滑块被拖动时触发onchange事件的新位置时才会触发onchange事件。

如何在Firefox中进行拖拽?

function showVal(newVal){ document.getElementById(“valBox”).innerHTML=newVal; } <span id=“valBox”></span> <输入类型=“范围” min=“5” max=“10” step=“1” onchange=“showVal(this.value)”>


当前回答

I'm posting this as an answer in case you are like me and cannot figure out why the range type input doesn't work on ANY mobile browsers. If you develop mobile apps on your laptop and use the responsive mode to emulate touch, you will notice the range doesn't even move when you have the touch simulator activated. It starts moving when you deactivate it. I went on for 2 days trying every piece of code I could find on the subject and could not make it work for the life of me. I provide a WORKING solution in this post.

移动浏览器和混合应用

Mobile browsers run using a component called Webkit for iOS and WebView for Android. The WebView/WebKit enables you to embed a web browser, which does not have any chrome or firefox (browser) controls including window frames, menus, toolbars and scroll bars into your activity layout. In other words, mobile browsers lack a lot of web components normally found in regular browsers. This is the problem with the range type input. If the user's browser doesn't support range type, it will fall back and treat it as a text input. This is why you cannot move the range when the touch simulator is activated.

点击这里阅读更多关于浏览器兼容性的内容

jQuery滑块

jQuery提供了一个滑块,以某种方式与触摸模拟工作,但它是起伏的,不是很光滑。它不能满足我,它可能不会为你,但你可以使它工作更顺利,如果你结合jqueryUi。

最佳解决方案:范围触摸

如果你在笔记本电脑上开发混合应用程序,有一个简单易用的库可以让范围类型输入与触摸事件一起工作。

这个库叫做Range Touch。

DEMO

有关此问题的更多信息,请查看这里的这个线程

为移动Safari (webkit)重新创建HTML5范围输入?

其他回答

I'm posting this as an answer in case you are like me and cannot figure out why the range type input doesn't work on ANY mobile browsers. If you develop mobile apps on your laptop and use the responsive mode to emulate touch, you will notice the range doesn't even move when you have the touch simulator activated. It starts moving when you deactivate it. I went on for 2 days trying every piece of code I could find on the subject and could not make it work for the life of me. I provide a WORKING solution in this post.

移动浏览器和混合应用

Mobile browsers run using a component called Webkit for iOS and WebView for Android. The WebView/WebKit enables you to embed a web browser, which does not have any chrome or firefox (browser) controls including window frames, menus, toolbars and scroll bars into your activity layout. In other words, mobile browsers lack a lot of web components normally found in regular browsers. This is the problem with the range type input. If the user's browser doesn't support range type, it will fall back and treat it as a text input. This is why you cannot move the range when the touch simulator is activated.

点击这里阅读更多关于浏览器兼容性的内容

jQuery滑块

jQuery提供了一个滑块,以某种方式与触摸模拟工作,但它是起伏的,不是很光滑。它不能满足我,它可能不会为你,但你可以使它工作更顺利,如果你结合jqueryUi。

最佳解决方案:范围触摸

如果你在笔记本电脑上开发混合应用程序,有一个简单易用的库可以让范围类型输入与触摸事件一起工作。

这个库叫做Range Touch。

DEMO

有关此问题的更多信息,请查看这里的这个线程

为移动Safari (webkit)重新创建HTML5范围输入?

还有另一种方法——在元素上设置一个标志,表明应该处理哪种类型的事件:

function setRangeValueChangeHandler(rangeElement, handler) {
    rangeElement.oninput = (event) => {
        handler(event);
        // Save flag that we are using onInput in current browser
        event.target.onInputHasBeenCalled = true;
    };

    rangeElement.onchange = (event) => {
        // Call only if we are not using onInput in current browser
        if (!event.target.onInputHasBeenCalled) {
            handler(event);
        }
    };
}

简介:

我在这里提供了一种无需jquery的跨浏览器桌面和移动设备的能力,以一致地响应范围/滑动条交互,这在当前的浏览器中是不可能的。它本质上迫使所有浏览器模仿IE11的on(“change”…事件为他们的on(“change”…或(“输入”……事件。新功能是…

function onRangeChange(r,f) {
  var n,c,m;
  r.addEventListener("input",function(e){n=1;c=e.target.value;if(c!=m)f(e);m=c;});
  r.addEventListener("change",function(e){if(!n)f(e);});
}

...r是范围输入元素,f是监听器。监听器将在任何改变范围/滑块值的交互之后被调用,但不会在没有改变该值的交互之后被调用,包括在当前滑块位置的初始鼠标或触摸交互,或者移动到滑块的任何一端时。

问题:

截至2016年6月初,不同的浏览器在响应范围/滑动条使用方面存在差异。有五个场景是相关的:

在当前滑块位置的初始鼠标向下(或触摸启动) 初始鼠标向下(或触摸启动)在一个新的滑块位置 在滑块上的1或2之后的任何后续鼠标(或触摸)移动 在滑块的任意一端经过1或2之后的任何后续鼠标(或触摸)移动 最后的鼠标移动(或触摸结束)

下表显示了至少三种不同的桌面浏览器在响应上述场景时的不同行为:

解决方案:

onRangeChange函数为范围/滑块交互提供了一致且可预测的跨浏览器响应。它迫使所有浏览器按照下表的方式运行:

In IE11, the code essentially allows everything to operate as per the status quo, i.e. it allows the "change" event to function in its standard way and the "input" event is irrelevant as it never fires anyway. In other browsers, the "change" event is effectively silenced (to prevent extra and sometimes not-readily-apparent events from firing). In addition, the "input" event fires its listener only when the range/slider's value changes. For some browsers (e.g. Firefox) this occurs because the listener is effectively silenced in scenarios 1, 4 and 5 from the above list.

(如果你真的需要在场景1,4和/或5中激活监听器,你可以尝试合并"mousedown"/"touchstart", "mousemove"/"touchmove"和/或"mouseup"/"touchend"事件。这样的解决方案超出了这个答案的范围。)

移动浏览器的功能:

我在桌面浏览器中测试了这段代码,但没有在任何移动浏览器中测试。然而,在本页的另一个回答中,MBourne表明了我在这里的解决方案“……似乎在我能找到的所有浏览器上都能运行(Win桌面:IE、Chrome、Opera、FF;Android Chrome, Opera和FF, iOS Safari)”。(谢谢MBourne。)

用法:

要使用此解决方案,请在您自己的代码中包含上面摘要(简化/缩小)或下面演示代码片段(功能相同但更不言自清)中的onRangeChange函数。按如下方式调用它:

onRangeChange(myRangeInputElmt, myListener);

其中myRangeInputElmt是你想要的<input type="range"> DOM元素,myListener是你想要在“change”类事件时调用的监听器/处理程序函数。

如果需要的话,你的监听器可以是无参数的,也可以使用event参数,也就是说,根据你的需要,以下任何一种都可以工作:

var myListener = function() {...

or

var myListener = function(evt) {...

(从输入元素中删除事件监听器(例如使用removeEventListener)没有在这个答案中解决。)

演示说明:

在下面的代码片段中,onRangeChange函数提供了通用的解决方案。代码的其余部分只是一个示例,用于演示它的使用。任何以my开头的变量…与通用解决方案无关,只是为了演示而呈现。

演示显示了范围/滑块值以及标准的“change”,“input”和自定义的“onRangeChange”事件触发的次数(分别是行A,行B和行C)。当在不同的浏览器中运行这个代码片段时,在与范围/滑块交互时注意以下事项:

在IE11中,场景2和场景3中,A行和C行值都发生变化,而B行值不变。 在Chrome浏览器和Safari浏览器中,场景2和场景3中B行和C行值都发生变化,场景5中A行值只发生变化。 在Firefox浏览器中,只有场景5中A行值发生变化,所有5个场景中B行值发生变化,场景2和场景3中C行值发生变化。 在上述所有浏览器中,C行(建议的解决方案)中的更改是相同的,即仅适用于场景2和3。

演示代码:

// main function for emulating IE11's "change" event: function onRangeChange(rangeInputElmt, listener) { var inputEvtHasNeverFired = true; var rangeValue = {current: undefined, mostRecent: undefined}; rangeInputElmt.addEventListener("input", function(evt) { inputEvtHasNeverFired = false; rangeValue.current = evt.target.value; if (rangeValue.current !== rangeValue.mostRecent) { listener(evt); } rangeValue.mostRecent = rangeValue.current; }); rangeInputElmt.addEventListener("change", function(evt) { if (inputEvtHasNeverFired) { listener(evt); } }); } // example usage: var myRangeInputElmt = document.querySelector("input" ); var myRangeValPar = document.querySelector("#rangeValPar" ); var myNumChgEvtsCell = document.querySelector("#numChgEvtsCell"); var myNumInpEvtsCell = document.querySelector("#numInpEvtsCell"); var myNumCusEvtsCell = document.querySelector("#numCusEvtsCell"); var myNumEvts = {input: 0, change: 0, custom: 0}; var myUpdate = function() { myNumChgEvtsCell.innerHTML = myNumEvts["change"]; myNumInpEvtsCell.innerHTML = myNumEvts["input" ]; myNumCusEvtsCell.innerHTML = myNumEvts["custom"]; }; ["input", "change"].forEach(function(myEvtType) { myRangeInputElmt.addEventListener(myEvtType, function() { myNumEvts[myEvtType] += 1; myUpdate(); }); }); var myListener = function(myEvt) { myNumEvts["custom"] += 1; myRangeValPar.innerHTML = "range value: " + myEvt.target.value; myUpdate(); }; onRangeChange(myRangeInputElmt, myListener); table { border-collapse: collapse; } th, td { text-align: left; border: solid black 1px; padding: 5px 15px; } <input type="range"/> <p id="rangeValPar">range value: 50</p> <table> <tr><th>row</th><th>event type </th><th>number of events </th><tr> <tr><td>A</td><td>standard "change" events </td><td id="numChgEvtsCell">0</td></tr> <tr><td>B</td><td>standard "input" events </td><td id="numInpEvtsCell">0</td></tr> <tr><td>C</td><td>new custom "onRangeChange" events</td><td id="numCusEvtsCell">0</td></tr> </table>

信贷:

虽然这里的实现主要是我自己的,但它是受到MBourne的回答的启发。另一个答案表明,“输入”和“更改”事件可以合并,生成的代码可以在桌面和移动浏览器中工作。然而,答案中的代码导致隐藏的“额外”事件被触发,这本身是有问题的,并且不同浏览器触发的事件不同,这是一个进一步的问题。我在这里的实现解决了这些问题。

关键词:

JavaScript输入类型范围滑块事件更改输入浏览器兼容性跨浏览器桌面移动无jquery

显然Chrome和Safari是错误的:onchange应该只在用户释放鼠标时被触发。要获得持续更新,您应该使用oninput事件,它将从Firefox、Safari和Chrome中从鼠标和键盘捕获实时更新。

然而,在IE10中不支持oninput,所以你最好的办法是将两个事件处理程序结合起来,像这样:

<span id="valBox"></span>
<input
  type="range"
  min="5"
  max="10"
  step="1"
  oninput="showVal(this.value)"
  onchange="showVal(this.value)"
/>

查看Bugzilla线程以获得更多信息。

Andrew Willem的解决方案并不兼容移动设备。

以下是他的第二个解决方案的修改,适用于Edge、IE、Opera、FF、Chrome、iOS Safari和移动设备(我可以测试):

更新1:删除“requestAnimationFrame”部分,因为我同意这是不必要的。

var listener = function() {
  // do whatever
};

slider1.addEventListener("input", function() {
  listener();
  slider1.addEventListener("change", listener);
});
slider1.addEventListener("change", function() {
  listener();
  slider1.removeEventListener("input", listener);
}); 

更新2:对安德鲁2016年6月2日的更新答案的回应:

谢谢,安德鲁-这似乎在我能找到的每一个浏览器(Win桌面:IE, Chrome, Opera, FF;Android Chrome, Opera和FF, iOS Safari)。

更新3:if ("oninput in slider)解决方案

下面的代码似乎可以跨上述所有浏览器运行。(我现在找不到原始来源。)我在用这个,但后来在IE上失败了,所以我去找了一个不同的,因此我在这里结束了。

if ("oninput" in slider1) {
    slider1.addEventListener("input", function () {
        // do whatever;
    }, false);
}

但在我检查你的解决方案之前,我注意到这在IE中又工作了-也许有一些其他的冲突。