简体   繁体   English

动态添加样式到香草js中的元素不起作用

[英]Adding styles dynamically to element in vanilla js not working

I am trying to customize the bar of an input of type range but, for some reason I didn't figure out yet, the backgroundImage is not being set. 我正在尝试自定义类型范围输入的栏,但是由于某种原因我还没有弄清楚,backgroundImage尚未设置。

What I am doing: 我在做什么:

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;
    }
})

Here is the code snippet: 这是代码片段:

 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]{ -webkit-appearance: none; /* fix for FF unable to apply focus style bug */ border: 1px solid white; } input[type=range]:focus { outline: none; } input[type=range]:focus::-webkit-slider-runnable-track { background: #ccc; } input[type=range]::-webkit-slider-thumb { -webkit-appearance: none; border: none; height: 30px; width: 30px; border-radius: 50%; background: #ffffff; margin-top: -11px; background-image: linear-gradient(rgba(255,255,255,0),rgba(0,0,0,0.1)); box-shadow: 0 0 8px rgba(0,0,0,0.3); } input[type=range]::-webkit-slider-runnable-track { height: 10px; background: #dddddd; border: none; border-radius: 3px; } 
 <input type="range" name="age" id="age" min="14" max="70" value="31"> 

Can you see what I am doing wrong? 你能看到我做错了吗? Why does the style.backgroundImage isn't being set? 为什么未设置style.backgroundImage? I debugged and saw that it is empty right after that line. 我调试了一下,发现在那一行之后它是空的。

Set your CSS properties in your CSS file. 在CSS文件中设置CSS属性。 If you want to change something dynamically/programatically via a script (JavaScript) your best bet is to toggle a separate CSS class on/off to make the change. 如果要通过脚本(JavaScript)动态/以编程方式更改某些内容,最好的选择是打开/关闭单独的CSS类以进行更改。 In other words try and use JS's .style method as little as possible. 换句话说,尝试并尽可能少地使用JS的.style方法。 Instead use JS's .classList . 而是使用JS的.classList

I just found out what I was doing wrong.. 我只是发现自己在做什么错..

I was forgetting to wrap inside url() and using this, instead event.target: 我忘记包装在url()内并使用它,而是使用event.target:

event.target.backgroundImage = 'url(-webkit-gradient(linear, left top, right top, '
                                                + 'color-stop(' + value + ', #e8972c), '
                                                + 'color-stop(' + value + ', #e6e6e6) '
                                                + '))';

Try this it will work for you. 试试这个,它将为您工作。

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,center bottom,center top,from(green),color-stop(0.5, yellow),to(blue))`;
    })

So the problem is not with javascript but your webkit-gradient syntax. 因此,问题不在于javascript,而在于您的webkit-gradient语法。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM