这里是新手。 我正在做我的第一个元素,我想为不同的人(普通人、 worker 、农民等)提供 slider ,但我不知道如何放置多个 slider 以及如何让它们全部工作。我从 W3schools 获取代码并更改了一些东西,但似乎我已经破坏了它并且我不知道如何处理它。我希望有 2 个 slider 都在工作,并且当一个 slider 上升而另一个 slider 下降时(将人员分配给他们的工作)。这是我的代码(我不知道它是否有任何帮助)
var people = 100;
var workers = 0;
document.onreadystatechange = function () {
if (document.readyState == "complete") {
var slider = document.getElementById("sliderWorkers");
var output = document.getElementById("workers").innerHTML = workers;
output.innerHTML = workers;
slider.oninput = function () {
output.innerHTML = this.value;
}
/*################################################################*/
var slider2 = document.getElementById("sliderPeople");
var output = document.getElementById("people").innerHTML = people;
output.innerHTML = slider.value;
slider.oninput = function () {
output.innerHTML = this.value;
}
}
}
setInterval(function () {
document.getElementById("people").innerHTML = people;
document.getElementById("workers").innerHTML = workers;
}, 100000);
.slider, .slider2 {
-webkit-appearance: none;
width: 10%;
height: 7px;
border-radius: 5px;
background: #d3d3d3;
outline: none;
-webkit-transition: .2s;
transition: opacity .2s;
}
.slider::-webkit-slider-thumb {
-webkit-appearance: none;
appearance: none;
width: 15px;
height: 15px;
border-radius: 50%;
background: black;
cursor: pointer;
}
.slider::-moz-range-thumb {
width: 15px;
height: 15px;
border-radius: 50%;
background: black;
cursor: pointer;
}
.slider2::-moz-range-thumb {
width: 15px;
height: 15px;
border-radius: 50%;
background: black;
cursor: pointer;
}
.slider2::-webkit-slider-thumb {
-webkit-appearance: none;
appearance: none;
width: 15px;
height: 15px;
border-radius: 50%;
background: black;
cursor: pointer;
}
<h1>Population range slider</h1>
<div class="slidecontainer">
<input type="range" min="0" max="100" value="0" class="slider" id="sliderWorkers">
<p>Value wokers: <div id="workers"></div>
</p>
</div>
<div class="slidecontainer">
<input type="range" min="0" max="100" value="100" class="slider2" id="sliderPeople">
<p>Value people: <div id="people"></div>
</p>
</div>
最佳答案
您可以使用 DOMContentLoaded和 addEventListener以便在 dom 准备就绪时开始您的任务。
在你之后querySelectorAll为了选择所有范围元素,并且每个元素都需要一个事件处理程序。
在此事件处理程序中,您可以更新值。
注意:P + DIV 现在是 P 里面有一个 SPAN。
document.addEventListener('DOMContentLoaded', function(e) {
document.querySelectorAll('[type="range"]').forEach(function (ele) {
ele.addEventListener('input', function (e) {
this.parentElement.querySelector('span').textContent = this.value;
var next = this.closest('div').nextElementSibling;
if (next.tagName != 'DIV') {
next = this.closest('div').previousElementSibling;
}
next.querySelector('[type="range"]').value = 100 - +this.value;
next.querySelector('span').textContent = 100 - +this.value;
});
// start with an initial value.....simultating an input...
ele.parentElement.querySelector('span').textContent = ele.value;
});
})
.slider {
-webkit-appearance: none;
width: 10%;
height: 7px;
border-radius: 5px;
background: #d3d3d3;
outline: none;
-webkit-transition: .2s;
transition: opacity .2s;
}
.slider::-webkit-slider-thumb {
-webkit-appearance: none;
appearance: none;
width: 15px;
height: 15px;
border-radius: 50%;
background: black;
cursor: pointer;
}
.slider::-moz-range-thumb {
width: 15px;
height: 15px;
border-radius: 50%;
background: black;
cursor: pointer;
}
<h1>Population range slider</h1>
<div class="slidecontainer">
<input type="range" min="0" max="100" value="0" class="slider" id="sliderWorkers">
<p>Value wokers: <span id="workers"></span></p>
</div>
<div class="slidecontainer">
<input type="range" min="0" max="100" value="100" class="slider" id="sliderPeople">
<p>Value people: <span id="people"></span></p>
</div>
关于javascript - 多个 slider 相互 react ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53946952/