1 回答
TA贡献1796条经验 获得超10个赞
您必须使用双等号 (==) 运算符来比较这些值。单个等号用于将值分配给左侧。
if (this.value == 0) {
output.innerHTML = '';
} else if (this.value == 1) {
output.innerHTML = '200k-400k';
} else if (this.value == 2) {
output.innerHTML = '400k-600k';
} else if (this.value == 3) {
output.innerHTML = '600k-800k';
} else if (this.value == 4) {
output.innerHTML = '800k-1m';
} else if (this.value == 5) {
output.innerHTML = '1m-2m';
} else if (this.value == 6) {
output.innerHTML = '2m+';
}
}
完整代码:
var slider = document.getElementById("myRange");
var output = document.getElementById("value");
slider.oninput = function() {
if (this.value == 0) {
output.innerHTML = '';
} else if (this.value == 1) {
output.innerHTML = '200k-400k';
} else if (this.value == 2) {
output.innerHTML = '400k-600k';
} else if (this.value == 3) {
output.innerHTML = '600k-800k';
} else if (this.value == 4) {
output.innerHTML = '800k-1m';
} else if (this.value == 5) {
output.innerHTML = '1m-2m';
} else if (this.value == 6) {
output.innerHTML = '2m+';
}
}
var start_value = slider.getAttribute("value");
var x = start_value;
var color = 'linear-gradient(90deg, rgb(117, 252, 117)' + (100 / 6) * x + '% , rgb(214, 214, 214)' + (100 / 6) * x + '%)';
slider.style.background = color;
slider.addEventListener("mousemove", function() {
x = slider.value;
color = 'linear-gradient(90deg, rgb(117, 252, 117)' + (100 / 6) * x + '% , rgb(214, 214, 214)' + (100 / 6) * x + '%)';
slider.style.background = color;
});
.main {
width: 1000px;
background-color: #888;
margin: 0 auto;
display: flex;
justify-content: center;
flex-direction: column;
border-radius: 10px;
border: 5px solid rgb(117, 252, 117);
}
h1 {
align-self: center;
font-size: 2em;
font-weight: 900;
}
p {
opacity: 0.7;
font-size: 2em;
font-weight: 900;
}
p span {
color: rgb(117, 252, 117);
}
.slideContainer {
width: 75%;
align-self: center;
}
.slider {
-webkit-appearance: none;
width: 100%;
height: 5px;
margin-bottom: 50px;
background: linear-gradient(90deg, rgb(117, 252, 117) 5%, rgb(214, 214, 214) 0%);
outline: none;
opacity: 0.7;
-webkit-transition: .2s;
transition: opacity .2s;
border-radius: 12px;
box-shadow: 0px 1px 10px 1px black;
}
.slider:hover {
opacity: 1;
}
.slider::-webkit-slider-thumb {
-webkit-appearance: none;
appearance: none;
width: 30px;
height: 30px;
background: white;
cursor: pointer;
border-radius: 50%;
}
.slider::-moz-range-thumb {
width: 35px;
height: 35px;
background: white;
cursor: pointer;
border-radius: 50%;
}
<div class="main">
<h1>Worth?</h1>
<div class="slideContainer">
<p>Value: <span id="value"></span></p>
<input type="range" min="0" max="6" value="0" class="slider" id="myRange">
</div>
</div>
添加回答
举报