I'm using the following function to calculate and save the percentage of how much of a web page I have scrolled:
const getPercentRead = () => {
const h = document.documentElement,
b = document.body,
st = "scrollTop",
sh = "scrollHeight";
return ((h[st] || b[st]) / ((h[sh] || b[sh]) - h.clientHeight)) * 100;
}
After refreshing the page, I want to use the output of this function and the current window height to scroll myself to the last saved location.
This is my current attempt at doing this:
const scrolled = (percentRead / windowHeight) * 100;
window.scrollTo(0, scrolled);
However, this does not take me to the correct scroll position. I'd appreciate any help you can give on this.
Thank you!