简体   繁体   English

创建范围滑块,不使用jQuery

[英]Creating a range slider, no jQuery

How would I go about creating a range slider, like shown below (only the horizontal one)? 我将如何创建一个范围滑块,如下图所示(仅水平滑块)? Everytime the user moves the slider the value should be sent to a JavaScript function. 每次用户移动滑块时,该值应发送到JavaScript函数。

滑块

I don't want to use jQuery or any other pre-made script. 我不想使用jQuery或任何其他预制脚本。 I want to code it myself so I understand it. 我想自己编写代码,以便理解。 No HTML5 either as it's rather new and a vast majority of people won't have HTML5 enabled browsers. 也没有HTML5,因为它是相当新的,而且绝大多数人不会使用支持HTML5的浏览器。

You can use 'range' input type of HTML5, 您可以使用HTML5的“范围”输入类型,

Minimum value<input type="range" id="myrange" min="1" max="100" step="1" onchange="updateValue(this.value);> Maximum value

Here is a javascript function which shows value: 这是一个显示值的javascript函数:

<script type="text/javascript">
function updateValue(val) {
  alert(val) ;
}

You mean something like this? 你的意思是这样的吗?

html(5) : html(5):

min<input type="range" id="slider" min="1" max="10" > max
<div id="output"> </div>

javascript : javascript:

window.addEventListener("load", function(){
     var slider = document.getElementById("slider");
     slider.addEventListener("change", function(){
         document.getElementById("output").innerHTML = "value : " + this.value;
    });
});

jsfiddle : http://jsfiddle.net/z39Ne/3/ jsfiddle: http : //jsfiddle.net/z39Ne/3/

It is pretty easy to follow. 很容易遵循。 If you have any questions just comment : ). 如果您有任何疑问,请评论:)。

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

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