I am currently working on a script to control the scrolling speed of elements on a webpage relative to their parent div. The code I have written so far is as follows:
$('#two').css({'top' : 600-($(this).scrollTop() / 1.2)+"px"});
The issue arises when my element is already positioned 600px from the top and if I refresh the page while at that exact position, it suddenly jumps to the top instead of maintaining its original position. In the console log, it shows that the top position is being calculated from 0 instead of the starting point of 600px where it should be.
I have attempted various methods such as using offsetHeight
and offset().top
, but none of them seem to solve the problem. Could anyone provide me with guidance on how to dynamically recalculate this position, so that even when refreshing the page from the middle, the element won't jump back to the top?