【问题标题】:Use input textbox to control input range slider with background color使用输入文本框控制具有背景颜色的输入范围滑块
【发布时间】:2018-11-30 05:13:06
【问题描述】:

我已关注 stackoverflow 上的一个问题并创建了一个范围滑块。但是,我希望它与文本框交互,这样当我输入数字时,滑块会改变,当我滑动滑块时,相对值将显示在文本框中。

这是我正在关注的解决方案:Link

以下是我的版本:

请任何人帮助并给我一些建议。非常感谢!

$('#slide-range').change(function () {
    var val = ($(this).val() - $(this).attr('min')) / ($(this).attr('max') - $(this).attr('min'));
    
    $(this).css('background-image',
                '-webkit-gradient(linear, left top, right top, '
                + 'color-stop(' + val + ', #A90F00), '
                + 'color-stop(' + val + ', #E2E2E2)'
                + ')'
                );


    });
#slide-range{
        outline-style:none;
        box-shadow:none;
        border-color:transparent;
        -webkit-appearance: none;
        -moz-apperance: none;
        border-radius: 8px;
        width: 334px;
        height: 8px;
        background-image: -webkit-gradient(
            linear,
            left top,
            right top,
            color-stop(1, #A90F00),
            color-stop(1, #E2E2E2)
        );
        margin-bottom: 30px;
    }
#slide-range::-webkit-slider-thumb {
        -webkit-appearance: none !important;
        background-color: #fff;
        height: 20px;
        width: 20px;
        border-radius: 20px;
        box-shadow:0 0 5px rgba(0,0,0,0.5);
    }
    
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<input id="slide-range" type="range" min="0" max="100" step="1" value="100">
<div class="input-amount">
  <input id="input-Amount" name="price" value="100">
  <span class="unit">$</span>
</div>

【问题讨论】:

    标签: jquery css html slider range


    【解决方案1】:

    在这种情况下,纯 CSS 会更好地工作。 这是可能帮助您解决的完整代码。

    $('#slide-range').on('input',function () {
      
      var newVal = $(this).val();
    
      $("#input-Amount").val(newVal);
    });
    $('#input-Amount').on('input', function(){
      //console.log($(this).val())
      $('#slide-range').val($(this).val())
    });
    input[type="range"]::-moz-range-progress {
      height:10px;
      border-radius:8px;
      background-image: -webkit-gradient(
        linear,
        left top,
        right top,
        color-stop(1, #A90F00),
        color-stop(1, #E2E2E2)
      );
      background-color: #43e5f7; 
      outline:none;
      border:0;
    }
    input[type="range"]::-moz-range-track {  
      height:10px;
      border-radius:8px;
      background-color: #D3D3D3;
      background-image: -webkit-gradient(
        linear,
        left top,
        right top,
        color-stop(1, #D3D3D3),
        color-stop(1, #D3D3D3)
      );
      outline:none;
      border:0;
      
    }
    <!DOCTYPE html>
    <html>
    <head>
      <meta charset="utf-8">
      <meta name="viewport" content="width=device-width">
      <title>JS Bin</title>
    </head>
    <body>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
    <input id="slide-range" type="range" min="0" max="100" step="1" value="100">
    <div class="input-amount">
      <input id="input-Amount" name="price" value="100">
      <span class="unit">$</span>
    </div>
    </body>
    </html>

    【讨论】:

      【解决方案2】:

      我认为这个插件可能有助于获得你想要的输出。

       <!DOCTYPE html>
          <html>
          <head>
          <title>slideControl.js jQuery Plugin</title>
          <link rel="stylesheet" type="text/css" href="slideControl.css" />
          <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.8.1/jquery.min.js"></script>
          <script type="text/javascript" src="https://www.jqueryscript.net/demo/Cool-SliderControl-Plugin/jquery.slideControl.js"></script>
      
          <script type="text/javascript">
          $(document).ready(function() {
              $('.slideControl').slideControl();
      
          });
          </script>
          </head>
          <body>
              <div class="content">
                  <ul class="clearfix">
                      <li><label>Foo: </label><input type="text" value="6.0" class="slideControl" maxlength="3" /></li>
                      <li><label>Bar: </label><input type="text" value="4.0" class="slideControl" maxlength="3" /></li>
                      <li><label>FooBar: </label><input type="text" value="9.0" class="slideControl" maxlength="3" /></li>
                  </ul>
              </div>
          </body>
          </html>
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2014-07-17
        • 1970-01-01
        • 2013-02-09
        • 2021-08-22
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多