Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

85
Vistas
Moving square in JS - Problem with offsetX / offsetY

I would like to ask you about help. I try to make a square that follows my mouse when I moving it, but something is wrong with my function and all the time after mouse move, the square is coming back to default position.

CODE:

let square = document.querySelector('.square')
let container = document.querySelector('.container')

container.addEventListener('mousemove', movingSquare);

function movingSquare(e) {
  square.style.transform = "translate" + "(" + e.offsetX + "px" + "," + e.offsetY + "px" + ")";
}
.container {
  background-color: red;
  height: 200px;
  width: 400px;
}

.square {
  background-color: blue;
  height: 50px;
  width: 50px;
}
<div class="container">
  <div class="square"> </div>
</div>

about 4 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

The lagging error does not occur when you move up or left, but occurs when you move down or right, this occurs when you move down or right, the containers mousemove event, doesn't get activated instead, the square receives the event, as when you move right/down your mouse falls on cursor

The below example is how your problem should look

let square = document.querySelector('.square')
let container = document.querySelector('.container')
container.addEventListener('mousemove', movingSquare);
function movingSquare(e) {
  square.style.transform = "translate" + "(" + e.offsetX + "px" + "," + e.offsetY + "px" + ")";
}
.container {
  background-color: red;
  height: 200px;
  width: 400px;
}

.square {
  background-color: blue;
  height: 50px;
  width: 50px;
}
<div class="container">
  <div class="square"> </div>
</div>


Solution

The square receives the event because it has pointer-events auto(true).
You can disable this in css by using pointer-events:none;

See the snippet below

let square = document.querySelector('.square')
let container = document.querySelector('.container')
container.addEventListener('mousemove', movingSquare);
function movingSquare(e) {
  square.style.transform = "translate" + "(" + e.offsetX + "px" + "," + e.offsetY + "px" + ")";
}
.container {
  background-color: red;
  height: 200px;
  width: 400px;
}

.square {
  background-color: blue;
  height: 50px;
  width: 50px;
  pointer-events: none;/*ADDED THIS LINE*/
}
<div class="container">
  <div class="square"> </div>
</div>

Refer here

about 4 years ago · Juan Pablo Isaza Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda