【问题标题】:Range slider with steps带步骤的范围滑块
【发布时间】:2017-04-28 22:03:44
【问题描述】:

我对范围滑块有疑问。我只想要这个值:1、3、5、10,但我的脚本运行不正常。

$(function(){
$('#boga').on('input',function(){
var hodnota=$(this).val(); 
if(hodnota<=5) 
$(this).attr("step","2");  
else {
$(this).attr("step","5");    
} 

});
}); 

 var max = 10,
 min = 1,
 step = 1,
 output = $('#output').text(min);
            
 $(".range-slider")
 .attr({'max': max, 'min':min, 'step': step,'value': String(min)})
  .on('input change', function() {               
   output.text(this.value);
  });
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<output id="output"></output>
 <input id="boga" class="range-slider" type="range">

我尝试将 else 中的“5”替换为“9”,它正在工作,但滑块跳到 1,之后跳到 10。

【问题讨论】:

    标签: jquery html slider rangeslider


    【解决方案1】:

    你可以试试这个脚本:

        const AVAILABLE_VALUES = [1, 3, 5, 10];
    
        const MAX = AVAILABLE_VALUES[AVAILABLE_VALUES.length - 1],
          MIN = AVAILABLE_VALUES[0];
    
    var output = $('#output').text(MIN);
    
        $(function() {
          var lastValue = MIN;
          $('#boga').on('input keydown', function(event) {
            var hodnota = parseInt($(this).val(), 10);
            if (event.keyCode) {
              // Keyboard navigation
              var indexOffset = 0;
              switch (event.keyCode) {
                case 38:
                case 39:
                  if (hodnota < MAX) {
                    indexOffset = 1;
                  }
                  break;
                case 37:
                case 40:
                  if (hodnota > MIN) {
                    indexOffset = -1;
                  }
                  break;
              }
              hodnota = AVAILABLE_VALUES[AVAILABLE_VALUES.indexOf(hodnota) + indexOffset];
            } else if ((AVAILABLE_VALUES.indexOf(hodnota) === -1)) {
              // Make dragging more snappy and distinctive
              hodnota = lastValue;
            }
    
            $(this).val(hodnota);
            output.text(hodnota);
            lastValue = hodnota;
          });
        });
    
        $(".range-slider")
          .attr({
            'max': MAX,
            'min': MIN,
            'value': String(MIN)
          });
    

    如果您不需要键盘导航,您可以省略 if (event.keycode) {..} 部分。

    如果您不想从 1..10 可视化整个比例尺,只想让用户在 1、3、5、10 值之间进行选择,您可以使用更简单的版本:

    const AVAILABLE_VALUES = [1, 3, 5, 10];
    
    const MAX = AVAILABLE_VALUES.length - 1,
      MIN = 0;
    
    var output = $('#output').text(AVAILABLE_VALUES[MIN]);
    
    $(function() {
      $('#boga').on('input', function(event) {
        var hodnota = parseInt($(this).val(), 10);
        $(this).attr('real-value', AVAILABLE_VALUES[hodnota]);
        output.text($(this).attr('real-value'));
      });
    });
    
    $(".range-slider")
      .attr({
        'max': MAX,
        'min': MIN,
        'value': String(MIN),
        'real-value': AVAILABLE_VALUES[0]
      });
    

    如果您有任何问题,请告诉我。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-07-24
      • 2018-11-23
      • 2021-10-20
      • 1970-01-01
      • 2018-05-31
      • 1970-01-01
      • 1970-01-01
      • 2017-04-08
      相关资源
      最近更新 更多