javascript - 之前的 HTML5 输入范围样式

标签 javascript html css

我目前正在使用以下代码设置 HTML5 输入类型范围的样式:

input[type="range"]{
    -webkit-appearance: none;
    -moz-apperance: none;
    width: 100%;
    height: 8px;
    padding: 0 20px;
    background: #024069;
    border-radius: 2px;
    margin-top: 25px;
}


input[type="range"]::-webkit-slider-thumb{
    -webkit-appearance:none; 
    -moz-apperance:none; 
    width:25px; 
    height:25px;
    -webkit-border-radius:20px; 
    -moz-border-radius:20px; 
    -ms-border-radius:20px; 
    -o-border-radius:20px; 
    border-radius:20px;
    background-image:-webkit-gradient(linear, left top, left bottom, color-stop(0, #fefefe), color-stop(0.49, #d7d7d7), color-stop(0.51, #d1d1d1), color-stop(1, #c8c8c8) );
    border: 1px solid #787878;
}

这一切都很好。但现在我想在实际 slider 上使用两种不同的颜色,因此拇指左侧是蓝色,拇​​指右侧是黑色。

我试过 :before 但没用。我们怎样才能做到这一点?

最佳答案

下面是一些 JavaScript,它会在您更改值时更新栏的颜色。

input.oninput = function () {
  var value = (input.value - input.min)/(input.max - input.min);
  input.style.backgroundImage = [
    '-webkit-gradient(',
      'linear, ',
      'left top, ',
      'right top, ',
      'color-stop(' + value + ', blue), ',
      'color-stop(' + value + ', red)',
    ')'
  ].join('');
};

onload = function() {
  var inputs = document.querySelectorAll('input[type=range]');
  for (var i = 0; i < inputs.length; i++) {
    input = inputs[i]
    input.oninput = function () {
      var value = (input.value - input.min)/(input.max - input.min);
      input.style.backgroundImage = [
        '-webkit-gradient(',
          'linear, ',
          'left top, ',
          'right top, ',
          'color-stop(' + value + ', blue), ',
          'color-stop(' + value + ', red)',
        ')'
      ].join('');
    };
  }
};
body {
    margin: 20px;
}
input[type="range"]{
    -webkit-appearance: none;
    -moz-apperance: none;
    width: 200px;
    height: 8px;
    padding: 0;
    border-left: 20px solid blue;
    border-right: 20px solid red;
    background: #024069;
    border-radius: 2px;
    margin-top: 25px;
    background-image: -webkit-gradient(
        linear,
        left top,
        right top,
        color-stop(0.2, blue),
        color-stop(0.2, red)
    );
    outline: none;
}


input[type="range"]::-webkit-slider-thumb{
    -webkit-appearance:none; 
    -moz-apperance:none; 
    width:25px; 
    height:25px;
    -webkit-border-radius:20px; 
    -moz-border-radius:20px; 
    -ms-border-radius:20px; 
    -o-border-radius:20px; 
    border-radius:20px;
    background-image:-webkit-gradient(linear, left top, left bottom, color-stop(0, #fefefe), color-stop(0.49, #d7d7d7), color-stop(0.51, #d1d1d1), color-stop(1, #c8c8c8) );
    border: 1px solid #787878;
}
<!doctype html>
<html>
  <body>
    <input type="range" min="1" max="100" step="1" value="20">
  </body>
</html>

感谢@adamvert 建议使用 oninput 而不是 onchange

关于javascript - 之前的 HTML5 输入范围样式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13913637/

相关文章:

javascript - 通过 JavaScript 让 YouTube 播放器全屏显示

html - 标题在 chrome 滚动时有闪烁问题?什么原因?

javascript - 更改特定 DOM 元素的 src

javascript - Vuex、vuejs - 未按预期对选择元素选项进行绑定(bind)

javascript onclick 未按预期触发

javascript - 如何使用 JQuery/CSS 在单击按钮时将 div 面板移动到所有其他 div 面板下方?

html - 使只读文本区域的高度由其内容决定

html - 是否有 CSS 可以允许元素跟随流,而 child 的位置为 :absolute?

javascript - 如何判断 JavaScript 中的引用是否已释放

javascript - 我刚刚将一些dojo文件编译在一起: How do I call a function inside the anonymous wrapper?