[英]Auto scroll top when page loaded using jQuery
I am trying to use jQuery to scroll the page automatically back to the top when the page is loaded. 我正在尝试使用jQuery在页面加载时自动滚动页面返回顶部。 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. 我也尝试将
$(window)
替换$(window)
$('html, body')
,遗憾的是它仍然不起作用。
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. 参数0,0是x和ycoördinates。
I hope it helps. 我希望它有所帮助。
The above solutions didn't work for me in Chrome. 以上解决方案在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'
. 事实上,有些浏览器会响应
'html'
,有些会回复'body'
。
PS. PS。 "800" is the duration of the animation.
“800”是动画的持续时间。
This is jQuery safe: 这是jQuery安全的:
< script type="text/javascript"> $(window).scrollTop(0); </script>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.