您好, 欢迎来到 !    登录 | 注册 | | 设为首页 | 收藏本站

如何使用JavaScript / jQuery滚动到页面顶部?

如何使用JavaScript / jQuery滚动到页面顶部?

哇,我这个问题迟到了9年。干得好:

将此代码添加到您的onload中。

// This prevents the page from scrolling down to where it was prevIoUsly.
if ('scrollRestoration' in history) {
    history.scrollRestoration = 'manual';
}
// This is needed if the user scrolls down during page load and you want to make sure the page is scrolled to the top once it's fully loaded. This has Cross-browser support.
window.scrollTo(0,0);

Chrome:受支持(自46开始)

Firefox:受支持(自46开始)

IE / Edge: (尚..)

Opera:受支持(自33起)

Safari:受支持

对于IE / Edge,如果要在其自动向下滚动后重新滚动到顶部,则对我有用:

var isIE11 = !!window.MSInputMethodContext && !!document.documentMode;
var isEdge = /Edge/.test(navigator.userAgent);
if(isIE11 || isEdge) {
    setTimeout(function(){ window.scrollTo(0, 0); }, 300);  // adjust time according to your page. The better solution would be to possibly tie into some event and trigger once the autoscrolling goes to the top.
}
javascript 2022/1/1 18:13:51 有498人围观

撰写回答


你尚未登录,登录后可以

和开发者交流问题的细节

关注并接收问题和回答的更新提醒

参与内容的编辑和改进,让解决方法与时俱进

请先登录

推荐问题


联系我
置顶