How to put javascript variable value in CSS?

0

I have a code on the site style page (css) to make an animation, but the animation time should vary according to a variable that is in javascript, how do I get the value of this variable to enter into my css code ?

css

div.progresso {
    animation: loading "tempo"s ease;
}

javascript

<script >
    var som1 = document.getElementById("player");
    som1.onloadeddata = function() {
    console.log("tempo:" + player.duration + "segundos");;
    var tempo = player.duration;
    <codigo pra colocar o valor da 'var tempo' naquele lugar especifico do css>
</script>

How could I do this?

    
asked by anonymous 10.12.2018 / 00:04

1 answer

0

You can use CSS's own variables and update via JavaScript. Ex.:

Defining the variable via CSS.

:root{
    --tempo-animacao: 1s;
}

div.progresso {
    animation: loading var(--tempo-animacao) ease;
}

Updating through JS.

var root = document.documentElement;

var som1 = document.getElementById("player");
som1.onloadeddata = function() {
   console.log("tempo:" + player.duration + "segundos");;
   var tempo = player.duration;
   root.style.setProperty('--tempo-animacao', tempo);
}    
    
11.12.2018 / 14:22