JQuery - .position isn't returning offset relative to parent
By : YinJRJ
Date : March 29 2020, 07:55 AM
I wish this help you If I am correct, I think this is a CSS issue, not necessarily a jQuery one.. My understanding is that the default value of the CSS "position" property is static (not absolute, relative), and positioning is calculated relative to the first ancestor that is not set to static. If the element has no ancestor with non-static position, the element will be positioned relative to the html block.
hope this fix your issue As mentioned in MDN HTMLElement.offsetTop returns position relative to the parent element . So in your example document.getElementById('div2-child').offsetTop returns top position relative to div2 So, if you want the position relative to window(viewport) you need to add child position with it's parent position like below: