sMohammad14
sMohammad14

Reputation: 43

event in javascript for moues position

Why this code NOT work for whole of body and not work smoothly?

function func (u){
  document.getElementsByClassName("x")[0].innerHTML= u.clientX;
  document.getElementsByClassName("y")[0].innerHTML= u.clientY;
}
div{
  float: left;
}
div:before{
  content: "Mouse Position >> ";
}
.x:before{
  content: "X: ";
}
.y:before{
  content: "Y: ";
}
p,div{
  display: inline;
}

body{
  width: 100vw;
  height: 100vw;
}
<body onmouseover="func(event)">
  <div>
    <p class="x"></p>
    <p class="y"></p>
  </div>
</body>

Upvotes: 0

Views: 46

Answers (2)

Mustafa Omar
Mustafa Omar

Reputation: 449

I think you want to update the values clientX, clientY when the mouse moves? If this is what you mean, so you should listen for mousemove event

Upvotes: 0

Majed Badawi
Majed Badawi

Reputation: 28414

Your explanation of the problem is not clear, but using onmousemove instead of onmouseover would make it continuous and more dynamic.

Upvotes: 2

Related Questions