Reputation: 555
I have tried to get this to work and was unsuccessful. I just want to have my mobile page views load at the top. I have tried the following.
// Method 1:
$('body').scrollTop(0);
// Method 2:
document.body.scrollTop = 0;
// Method 3:
$(window).on('beforeunload', function(){
$(window).scrollTop(0);
});
I tried here first and was unsuccessful.
I just want to always start at the top of the page when I click a link in mobile. I have no idea as to why none of these solutions work.
Upvotes: 0
Views: 1700
Reputation: 7248
You can use also vanilla JS for it:
document.body.scrollTop = document.documentElement.scrollTop = 0;
or with JQuery
$(document).ready(function() {
$(this).scrollTop(0);
});
Try also
$('html,body').scrollTop(0);
Check this demo as well, might help https://jsfiddle.net/a_incarnati/v575uvyb/2/
Upvotes: 2
Reputation: 3819
You should include $(this).scrollTop(0);
within a $(document).ready();
call, like so:
$(document).ready(function() {
$(this).scrollTop(0);
});
In your code, you are calling $(this).scrollTop(0);
before the page unloads, rather than after the page and DOM have been reloaded.
Upvotes: 1