Label with mouse cursor

2

I created a label with the position of the cursor, I want this label to always be held by the cursor. I created this, it works, but not behind the cursor.

var x = ev.clientX;
var y = ev.clientY;
var info_souris = document.getElementById("position_souris");
info_souris.innerHTML = x + "," + y; 


<label id="position_souris"></label>
    
asked by anonymous 15.12.2014 / 17:51

1 answer

2

You need to add a callback to the mousemove event, try:

window.onload = function() {
  document.addEventListener('mousemove', function(e) {
    var x = e.clientX,
        y = e.clientY;

    var info_souris = document.getElementById("position_souris");
    info_souris.innerHTML = x + "," + y; 
    info_souris.style.top = y + 'px';
    info_souris.style.left = x + 'px';
  });
};
label#position_souris {
  position: absolute;
}
<label id="position_souris">aqui</label>
    
15.12.2014 / 18:00