Label with mouse cursor

Asked

Viewed 63 times

2

I created a label with the cursor position, I want that label to be always grabbed cm 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>

1 answer

2


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

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>

  • Right, but I wish the label was always behind the cursor.

  • Ahhh yeah, got it, hold on then

  • Is that what you want? Look at my @akm edition

  • 1

    That’s right, thank you

Browser other questions tagged

You are not signed in. Login or sign up in order to post.