Search code examples
jqueryscrollpositionoffsetonscroll

position.top of a div changing constantly on scroll


i have to calculate the position().top of a series of divs inside of a self scrolling container.

the position is correctly calculated on load, but it constantly changes while scrolling.

here's a pen: https://codepen.io/frontend2020/pen/GRWJVvq (open the console to see the number changing on scroll)

i would need that initial number to never change, is this possible?

if I apply the same logic to $(document).on("scroll", xxx); instead of $(".container").on("scroll", xxx); that number never changes, and that's how I would want that to be in my case as well.

any hint or solution is greatly appreciated


Solution

  • Use offsetTop instead of position().top; Here is the example codepen link