简体   繁体   English

更改HTML5中的步骤 <input type=“range”> 有一定价值

[英]Change step in HTML5 <input type=“range”> on certain value

I'm trying to do an HTML5 <input type="range"> that has: 我正在尝试做一个HTML5 <input type="range"> ,它具有:

  • Minimum value: 50 最小值:50
  • Maximum value: 2000 最大值:2000
  • Default value: 50 默认值:50
  • Step: 50 步骤:50

<input name="flevel_gigas" id="flying" type="range" min="50" max="2000" value="50">

When the user passes 500, the step of the input will change from 50 to 250 up to 2000. 当用户通过500时,输入步长将从50更改为250,直至2000。

I'm doing it with JQuery. 我正在使用JQuery。 The code is: 代码是:

<script>
    $(function(){
        function returnStep(value) {
            var step;
            if(value<500){
                step=50;
            }
            else{
                step=250;
            }
            return step;
        }
        $('input[name="flevel_gigas"]').on("input change",function(){
            var currentValue = $(this).val();
            $(this).attr('step', returnStep(currentValue));
        });
    });
</script>

But it not works. 但这行不通。 When the slider is at 450 it jumps to 550 then the step changes to 250 and starts counting correctly but only until 1800 (because 1800 plus 250 is 2050 and it exceeds 2000). 当滑块在450处时,它跳到550,然后步长更改为250,并开始正确计数,但是直到1800(因为1800加250为2050,并且超过2000)。

Could anyone help me where is the error or the misunderstanding of the range's perfomance? 有人可以帮助我该范围性能的错误或误解在哪里?

The problem here is that valid numbers are min + N * step for some integer N . 这里的问题是,对于某个整数N ,有效数字是min + N * step

In your case, valid numbers are (with the bigger step) 50 + N * 250 , which is 300 (not seen by the user in your case) 550, 800 and so on. 在您的情况下,有效数字是(更大的步骤) 50 + N * 250 ,即300 (在您的情况下用户看不到) 550, 800依此类推。

You will need to change the min to 0 when your step changes - it's a bit of a hack but it will provide a more functional "start value" for your increased step. 当您更改step时,您需要将min更改为0这有点麻烦,但是它将为您增加的步长提供更具功能性的“起始值”。

you can check this code 您可以检查此代码

html code is this , set min value 0 html代码是this,将最小值设置为0

<input name="flevel_gigas" id="flying" type="range" min="0" max="2000" value="50">

and jQuery Code 和jQuery代码

 <script>
  $(function(){
    function returnStep(value) {
        var step;
        if(value<500){
            step=50;
        }
        else{
            step=250;
        }
        return step;
    }
    $('input[name="flevel_gigas"]').on("input change",function(){
        var currentValue = $(this).val();
       var step =  returnStep(currentValue);
        $(this).attr('step', step);
    });
});

</script>

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

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