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

209
Visualizações
Preserve mouse offset to parent when clicking

I'm trying to get the mouse offset inside a square, but when I click on its children, the offset changes, now returning the mouse offset inside the children, and not the parent.

What I would like to achieve is to get the mouse offset inside the parent, even if I click inside the children. Thanks a lot!

Below is an example of the problem. (Look in the console)

$("#parent").click(function(e){
   console.log(e.offsetX, e.offsetY)
});
.square{
  position: relative;
  height: 200px;
  width: 200px;
  background-color: red;
}

.small{
  position: absolute;
  top: 25px;
  right: 25px;
  height: 50px;
  width: 50px;
  background-color: blue;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div class="square" id="parent">
  <div class="square small"></div>
</div>

about 4 years ago · Juan Pablo Isaza
3 Respostas
Responde à pergunta

0

You can use

e.target

to get the element clicked, as this is the parent element that the click has bubbled up to.

Then combine with .getBoundingClientRect() to get its position, gives:

$("#parent").click(function(e) {
  var rect = e.target.getBoundingClientRect();

  //console.log(e.offsetX, e.offsetY)
  //console.log(rect.left, rect.top)

  console.log(rect.left + e.offsetX, rect.top + e.offsetY)
});
.square {
  position: relative;
  height: 200px;
  width: 200px;
  background-color: red;
}

.small {
  position: absolute;
  top: 25px;
  right: 25px;
  height: 50px;
  width: 50px;
  background-color: blue;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div class="square" id="parent">
  <div class="square small"></div>
</div>

about 4 years ago · Juan Pablo Isaza Relatório

0

Adding pointer-events: none; to the css for the class "small" disables clicking on the element.

$("#parent").click(function(e){
   console.log(e.offsetX, e.offsetY)
});
.square{
  position: relative;
  height: 200px;
  width: 200px;
  background-color: red;
}

.small{
  position: absolute;
  top: 25px;
  right: 25px;
  height: 50px;
  width: 50px;
  background-color: blue;
  pointer-events: none;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div class="square" id="parent">
  <div class="square small"></div>
</div>

about 4 years ago · Juan Pablo Isaza Relatório

0

Solution

Add a transparent background and full height and width child div to the parent (#parent-overlay) and check for its offset instead.

Why

The offsets are gotten from the click event and the click event was on the child. So while in the child, you can't get an offset from the parent (the event was registered on the child).

Adding a full width and height overlay, that matches the parent, and getting offset from there should give you what you want.

$("#parent-overlay").click(function(e) {
  console.log(e.offsetX, e.offsetY);
});
.square{
  position: relative;
  height: 200px;
  width: 200px;
  background-color: red;
}

.small{
  position: absolute;
  top: 25px;
  right: 25px;
  height: 50px;
  width: 50px;
  background-color: blue;
  z-index: 1;
}

#parent-overlay {
  position: absolute;
  z-index: 3;
  top: 0;
  left: 0;
  bottom: 0;
  right: 0;
  background-color: transparent;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div class="square" id="parent">
  <div id="parent-overlay"></div>
  <div class="square small"></div>
</div>

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