Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

156
Views
How can I subscribe to or detect position change of HTML element?

I want subscribe/detect position change of html element caused by every event (scrolling, DOM mutation, CSS/style changes).

If the position of a particular element changes for every reason I want detect it!

this is just an example

const checkPosition = (el, callback) => {

  let rect = el.getBoundingClientRect();

  let x = rect.x;
  let y = rect.y;
  
  const check = () => {
    let rect = el.getBoundingClientRect();
    if (rect.x !== x || rect.y !== y) {
      x = rect.x; 
      y = rect.y;
      callback(x, y);
    } 
    requestAnimationFrame(check);
  };
  check();
};


const divVar = document.getElementById('track');
checkPosition(divVar, (x, y) => console.log('moved', {x, y}));
#track {
  display: block; 
  height: 50px; 
  width: 50px; 
  border: 1px solid red;
}

#container {
  dispay: block; 
  height: 100px; 
  width: 100px; 
  overflow: auto; 
}
<div id="container">
    <br><br><br><br><br><br><br><br><br>
    <div id="track"></div>
    <br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br>
</div>

how detect when getBoundingClientRect() changes?

about 4 years ago · Juan Pablo Isaza
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!