I have an issue with .scrollTop()
I have a page that stores the current scrollTop() position in session when unloading.
My goal is that when I come back to this page, the page scrolls until the last scrollTop position I had on it.
My JS code is simple :
console.log('=>'+parseInt(scrollto)+' '+parseInt($(window).scrollTop()));
$(window).scrollTop(scrollto);
console.log('=>'+parseInt(scrollto)+' '+parseInt($(window).scrollTop()));
Result in my browser’s console :
=> 2500 0
=> 2500 2500
Ok, my page is scrolled at the exact last postion.
The issue comes with big values of scrollto variable.
This is what I’ve got in console :
=> 12352 0
=> 12352 7683
I don’t get why it is not scrolling at the requested position
I’ve even tried this :
var max_times= 10;
for(var i=0;i<max_times;i++){
console.log('=>'+parseInt(scrollto)+' '+parseInt($(window).scrollTop()));
if (parseInt(scrollto) > parseInt($(window).scrollTop()))
$(window).scrollTop(scrollto);
}
And result :
=> 13450 0
=> 13450 7985
=> 13450 7985
=> 13450 7985
...
=> 13450 7985
Other info :
- I have the same result on Chrome and Safari.
- The page I’m talking about contains a lot of images so it takes some
time to be fully loaded.
I would really appreciate any help.
Thank you.
Ok.
I’ve found a solution that works.
Thanks for your hints guys, the total height of the page had to be involved.
What I’ve done is that :
I set a interval that checks if the total height of the page is enough to scroll to the requested position each 500ms. If it’s the case, it scrolls. If the position is OK, then we clear the interval.
If none of this works, the interval is cleared after 10s.
Thanks again for helping me out with this !