I am trying to use jQuery to scroll the page automatically back to the top when the page is loaded. Here is my code:
<script type="text/javascript">
$(document).ready(function () {
$(window).scrollTop(0);
return false;
});
</script>
However, the code does not work. I have also tried to replace $(window)
to $('html, body')
, sadly it still does not work.
So could anyone advice on this? Thanks a lot!
Try this
<script type="text/javascript">
$(document).ready(function () {
window.scrollTo(0,0);
});
</script>
The parameters 0,0 are the x and y coördinates.
I hope it helps.
The above solutions didn't work for me in Chrome. This is what I had the most success with:
$(window).on('beforeunload', function() {
$('body').fadeOut(225);
});
Is easier and more reliable if you use this solution:
<script type="text/javascript">
$('html,body').animate({scrollTop:0},800);
</script>
In fact some browsers will respond to 'html'
and some to 'body'
.
PS. "800" is the duration of the animation.
This is jQuery safe:
< script type="text/javascript"> $(window).scrollTop(0); </script>
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.