CSS recognizing Javascript variables within html, is it possible?

3

For example, I have a script that takes the mouse coordinates:

 function posicaoMouse(ev){
        ev.preventDefault();
        var x = ev.clientX;
        var y = ev.clientY;
        console.log(x);
        console.log(y);
    }

And I would like the top and left to be defined by the x and y variables of the function posicaoMouse , is it possible? <span class="duvida" id="duvida2" style="position: absolute; top:'$x' left:'$y'"> teste </span>

    
asked by anonymous 20.10.2017 / 14:05

1 answer

6

You can change the value of the element property by javascript:

let elem = documento.getElementById("duvida2");
function posicaoMouse(ev){
    ev.preventDefault();
    var x = ev.clientX;
    var y = ev.clientY;
    elem.style.top = x;
    elem.style.left = y;
    console.log(x);
    console.log(y);
}
    
20.10.2017 / 14:12