范围滑块显示多个输出

时间:2019-07-17 10:36:28

标签: javascript jquery html css

我希望获得有关此范围滑块以提供多个输出的帮助。我尝试了几种不同的方法,但似乎无法弄清楚。

如果数字达到千,我希望“ 590”文本为滑块值的5.9倍,带逗号。 我希望“ 30,000”文本也应为滑块值的300倍,并且还要加上逗号

[JSFiddle演示]

var elem = document.querySelector('input[type="range"]');

var rangeValue = function(){
  var newValue = elem.value;
  var target = document.querySelector('.value');
  target.innerHTML = newValue;
}

elem.addEventListener("input", rangeValue);
@import url(https://fonts.googleapis.com/css?family=Work+Sans:300);
body {
  font-family: "Work Sans", Helvetica, Arial, sans-serif; 
  color: #d7d7d7;
  padding-top: 40px;
  text-shadow: white 1px 1px 1px;
}
.value {
  text-align: center;
  font-weight: 300;
  font-size: 10em;
  width: 300px; 
  height: 100px;
  line-height: 60px;
  letter-spacing: -.03em;
  margin: 40px auto;
}
input[type="range"] {
  display: block;
  -webkit-appearance: none;
  background-color: #ffa842;
  width: 300px;
  height: 5px;
  border-radius: 5px;
  margin: 0 auto;
  outline: 0;
}
input[type="range"]::-webkit-slider-thumb {
  -webkit-appearance: none;
  background-color: #f5a623;
  width: 30px;
  height: 30px;
  border-radius: 50%;
  border: 2px solid white;
  cursor: pointer;
  transition: .3s ease-in-out;
}​
  input[type="range"]::-webkit-slider-thumb:hover {
    background-color: white;
    border: 2px solid #f5a623;
  }
  input[type="range"]::-webkit-slider-thumb:active {
    transform: scale(1.6);
  }
<div class="value">100</div>
<input type="range" min="100" max="1000" step="10" value="100">

<br><br>
<strong><span class="perYear" id="perYear" style=" font-size: 70px; font-weight: 700; color: #ff9600;">590</span></strong>
<br><br>
<strong><span class="perTwentyFive" id="perTwentyFive" style=" font-size: 70px; font-weight: 700; color: #ff9600;">30,000</span></strong>

1 个答案:

答案 0 :(得分:0)

我在JavaScript中添加了几行。

当范围值更改时,您可以计算5.9 * newValue。基于this answer插入逗号I。使用parseInt()进行四舍五入,以避免出现诸如1003.0000000000001之类的怪异数字。

var elem = document.querySelector('input[type="range"]');

var rangeValue = function(){
  var newValue = elem.value;
  document.querySelector('.value').innerHTML = newValue;
  var perYear = insertCommas(5.9 * newValue);
  document.querySelector("#perYear").innerHTML = perYear;
  var perTwentyFive = insertCommas(300 * newValue);
  document.querySelector("#perTwentyFive").innerHTML = perTwentyFive;
}

elem.addEventListener("input", rangeValue);

function insertCommas(x) {
  return parseInt(x).toString().replace(/\B(?=(\d{3})+(?!\d))/g, ",");
}
@import url(https://fonts.googleapis.com/css?family=Work+Sans:300);
body {
  font-family: "Work Sans", Helvetica, Arial, sans-serif; 
  color: #d7d7d7;
  padding-top: 40px;
  text-shadow: white 1px 1px 1px;
}
.value {
  text-align: center;
  font-weight: 300;
  font-size: 10em;
  width: 300px; 
  height: 100px;
  line-height: 60px;
  letter-spacing: -.03em;
  margin: 40px auto;
}
input[type="range"] {
  display: block;
  -webkit-appearance: none;
  background-color: #ffa842;
  width: 300px;
  height: 5px;
  border-radius: 5px;
  margin: 0 auto;
  outline: 0;
}
input[type="range"]::-webkit-slider-thumb {
  -webkit-appearance: none;
  background-color: #f5a623;
  width: 30px;
  height: 30px;
  border-radius: 50%;
  border: 2px solid white;
  cursor: pointer;
  transition: .3s ease-in-out;
}​
  input[type="range"]::-webkit-slider-thumb:hover {
    background-color: white;
    border: 2px solid #f5a623;
  }
  input[type="range"]::-webkit-slider-thumb:active {
    transform: scale(1.6);
  }
<div class="value">100</div>
<input type="range" min="100" max="1000" step="10" value="100">

<br><br>
<strong><span class="perYear" id="perYear" style=" font-size: 70px; font-weight: 700; color: #ff9600;">590</span></strong>
<br><br>
<strong><span class="perTwentyFive" id="perTwentyFive" style=" font-size: 70px; font-weight: 700; color: #ff9600;">30,000</span></strong>