Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

81
Visualizações
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 Respostas
Responde à pergunta

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda