【发布时间】:2021-06-23 17:22:36
【问题描述】:
我正在为我的网站开发自定义范围滑块。一切正常,但我坚持使单击特定框滑块(input[type="range"]) 时可以根据该特定 div 的数据标签值进行更新,这样我们就不必拖动滑块的拇指来更新自定义栏? .附件是源代码,我在哪里分享了我到目前为止所取得的成就以及剩下的内容,为了方便起见,我将范围输入的不透明度设置为 0.1,这样你就可以了解后端的情况。任何建议或帮助将不胜感激。谢谢!
const rangeSlider = document.querySelector('#price_slider');
rangeSlider.addEventListener("input", rangeScript);
const customProgress = document.querySelector('#customProgress');
for(let i = 0; i < rangeSlider.max - rangeSlider.min ; i++){
const step = document.createElement('div');
step.classList.add('step');
step.setAttribute('data-label', +rangeSlider.min + i + 1);
customProgress.appendChild(step);
}
customProgress.querySelector(`.step[data-label="${rangeSlider.value}"]`)
.classList.add('current')
function rangeScript(value){
const target = document.getElementById('progress');
let newValue = parseInt(this.value);
const currentStep = customProgress.querySelector(`.step.current`);
if (currentStep) {
currentStep.classList.remove('current');
}
nextStep = customProgress.querySelector(`.step[data-label="${newValue}"]`);
if (nextStep) {
nextStep.classList.add('current')
}
}
#customProgress {
display: flex;
width: 100%;
height: 25px;
background: transparent;
}
.step {
position: relative;
background: yellow;
height: 100%;
width: 100%;
border-right: 1px solid black;
}
.step::after {
content: attr(data-label);
position: absolute;
top: -1em;
right: -.25em;
}
.step ~ .current,
.step.current {
background: blue;
}
<h1>what I have achieved</h1>
<div id="customProgress">
</div>
<div id="progress" style=" width: 100%;" >
<input id="price_slider" type="range" min="4" max="9" value="" style="opacity: 0.1; width: 100%; height: 50px" />
</div>
【问题讨论】:
标签: javascript html css