我想自定义类型范围的输入栏,但出于某种原因,我还没有弄清楚,backgroundImage没有被设置。动态添加样式到香草js不工作的元素
我在做什么:
var age = document.querySelector('#age');
var ageResult = document.querySelector('#age-result');
ageResult.textContent = age.value;
age.addEventListener('change', function(event) {
var value = (event.target.value - event.target.min)/(event.target.max - event.target.min);
this.style.backgroundImage = '-webkit-gradient(linear, left top, right top, '
+ 'color-stop(' + value + ', #e8972c), '
+ 'color-stop(' + value + ', #e6e6e6) '
+ ')';
if (event.target.tagName === 'INPUT') {
ageResult.textContent = event.target.value;
}
})
这里是代码片段:
var age = document.querySelector('#age');
age.addEventListener('change', function(event) {
var value = (event.target.value - event.target.min)/(event.target.max - event.target.min);
this.style.backgroundImage = '-webkit-gradient(linear, left top, right top, '
+ 'color-stop(' + value + ', #e8972c), '
+ 'color-stop(' + value + ', #e6e6e6) '
+ ')';
})
input[type=range]{
\t -webkit-appearance: none;
\t /* fix for FF unable to apply focus style bug */
\t border: 1px solid white;
}
input[type=range]:focus {
\t outline: none;
}
input[type=range]:focus::-webkit-slider-runnable-track {
\t background: #ccc;
}
input[type=range]::-webkit-slider-thumb {
\t -webkit-appearance: none;
\t border: none;
\t height: 30px;
\t width: 30px;
\t border-radius: 50%;
\t background: #ffffff;
\t margin-top: -11px;
\t background-image: linear-gradient(rgba(255,255,255,0),rgba(0,0,0,0.1));
\t box-shadow: 0 0 8px rgba(0,0,0,0.3);
}
input[type=range]::-webkit-slider-runnable-track {
\t height: 10px;
\t background: #dddddd;
\t border: none;
\t border-radius: 3px;
}
<input type="range" name="age" id="age" min="14" max="70" value="31">
你能看到什么,我做错了什么?为什么没有设置style.backgroundImage?我调试了一下,发现它在该行之后是空的。
我不能在这种情况下..我必须在用户更改范围时自定义栏,所以我正在动态计算backgroundImage的值 – Periback
您知道为什么没有设置它吗? – Periback
我刚刚发现我做错了什么。 – Periback