我已将页面向下滚动到中间,然后重新加载页面 (F5)。在那个滚动之后仍然在中间。这是正常的。我的任务是在页面加载后设置 scrollY = 0 。
如果我设置 document.documentElement.scrollTop = 0 (document.body.scrollTop = 0 or window.pageXOffset = 0 and inside or out of $(document).ready(...) or $(window).load(...)
) 页面不会滚动到顶部。
我有这个功能:
function getScrollXY() {
var x = 0, y = 0;
if( typeof( window.pageYOffset ) == 'number' ) {
// Netscape
x = window.pageXOffset;
y = window.pageYOffset;
} else if( document.body && ( document.body.scrollLeft || document.body.scrollTop ) ) {
// DOM
x = document.body.scrollLeft;
y = document.body.scrollTop;
} else if( document.documentElement && ( document.documentElement.scrollLeft || document.documentElement.scrollTop ) ) {
// IE6 standards compliant mode
x = document.documentElement.scrollLeft;
y = document.documentElement.scrollTop;
}
return [x, y];
}
即使在页面加载后滚动页面,它也总是返回 x=0。
那么如何在页面加载后将滚动设置为顶部?
jsfiddle上的代码:http: //jsfiddle.net/g6w9r/1/
从 jsfiddle 中查看结果的框架:http: //fiddle.jshell.net/g6w9r/1/show/