如何使用JavaScript / jQuery滚动到页面顶部? [英] How to scroll to top of page with JavaScript/jQuery?

查看:105
本文介绍了如何使用JavaScript / jQuery滚动到页面顶部?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有一种方法来控制JavaScript / jQuery的浏览器滚动?



当我将页面向下滚动一半时,然后触发重新加载,我希望页面去打包,但它试图找到最后一个滚动位置。所以我做到了这一点:$ b​​
$ b $ $ $ $ $ $'$'$''''。ready(function(){
$(window) .scrollTop(0);
});

但没有运气。

编辑



因此,当我在页面加载后调用它们时,您的答案都能正常工作 - 谢谢。但是,如果我只是在页面上进行刷新,看起来像浏览器计算并滚​​动到它的旧滚动位置后面 .ready 事件(我测试了body onload()功能也是如此)。

所以后续是,有没有办法阻止浏览器滚动到过去的位置,或者重新滚动到顶部之后它的东西?

解决方案

跨浏览器,纯JavaScript解决方案:

  document.body.scrollTop = document.documentElement.scrollTop = 0; 


Is there a way to control browser scrolling with JavaScript/jQuery?

When I scroll my page half way down, then trigger a reload, I want the page to go pack to the top, but instead it tries to find the last scroll position. So I did this:

$('document').ready(function() {
   $(window).scrollTop(0);
});

But no luck.

EDIT:

So both your answers worked when I call them after the page loads-Thanks. However, if I just do a refresh on the page, looks like the browser calculates and scrolls to its old scroll position AFTER the .ready event (I tested the body onload() function too).

So the follow up is, is there a way to PREVENT the browser scrolling to its past position, or to re-scroll to the top AFTER it does its thing?

解决方案

Cross-browser, pure JavaScript solution:

document.body.scrollTop = document.documentElement.scrollTop = 0;

这篇关于如何使用JavaScript / jQuery滚动到页面顶部?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆