如果您在页面a上滚动并刷新页面,则该页面将在您离开的位置处刷新。这很好,但是在URL中存在锚点位置的页面上也会发生这种情况。例如,如果您单击链接http://example.com/post/244#comment5
,并在浏览后刷新页面,则您不会在锚点处,页面会出现跳动。是否有任何方法使用javascript来防止这种情况?以便无论如何,您始终可以导航到锚点。
如果您在页面a上滚动并刷新页面,则该页面将在您离开的位置处刷新。这很好,但是在URL中存在锚点位置的页面上也会发生这种情况。例如,如果您单击链接http://example.com/post/244#comment5
,并在浏览后刷新页面,则您不会在锚点处,页面会出现跳动。是否有任何方法使用javascript来防止这种情况?以便无论如何,您始终可以导航到锚点。
在Chrome浏览器中,即使您将scrollTop强制设置为0,它仍会在第一个滚动事件后跳跃。
您应该将滚动绑定到以下内容:
$(window).on('beforeunload', function() {
$(window).scrollTop(0);
});
因此,浏览器被欺骗认为在刷新之前的起始位置。window.onbeforeunload = function(){ window.scrollTo(0,0); }
翻译为:在没有使用jQuery的情况下:
window.onbeforeunload = function(){ window.scrollTo(0,0); }
- ProfNandaa经过多次尝试,我终于成功了。在这里anzo是正确的,因为使用beforeunload
会使页面在用户重新加载页面或单击链接时跳转到顶部。所以unload
是明显的方法。
$(window).on('unload', function() {
$(window).scrollTop(0);
});
Javascript方法(感谢ProfNandaa):
window.onunload = function(){ window.scrollTo(0,0); }
编辑:2015年7月16日
即使使用unload
事件,Firefox仍存在跳转问题。
beforeunload
解决方案好得多,因为它可以防止在重新加载和单击锚点时跳转到页面顶部。 - BradGreens由于浏览器行为的更改,不再推荐使用此解决方案。请查看其他答案。
基本上,如果使用锚点,我们会绑定到窗口滚动事件。其主要思想是第一个滚动事件必须属于浏览器自动重新定位。当这种情况发生时,我们进行自己的重新定位,然后删除绑定的事件。这可以防止随后的页面滚动破坏系统。
$(document).ready(function() {
if (window.location.hash) {
//bind to scroll function
$(document).scroll( function() {
var hash = window.location.hash
var hashName = hash.substring(1, hash.length);
var element;
//if element has this id then scroll to it
if ($(hash).length != 0) {
element = $(hash);
}
//catch cases of links that use anchor name
else if ($('a[name="' + hashName + '"]').length != 0)
{
//just use the first one in case there are multiples
element = $('a[name="' + hashName + '"]:first');
}
//if we have a target then go to it
if (element != undefined) {
window.scrollTo(0, element.position().top);
}
//unbind the scroll event
$(document).unbind("scroll");
});
}
});
这对我来说有效。
//Reset scroll top
history.scrollRestoration = "manual"
$(window).on('beforeunload', function(){
$(window).scrollTop(0);
});
下面是一种更通用的方法。与其试图阻止浏览器滚动(或看起来像是跳转到顶部),我只需恢复页面上先前的位置。即,我将当前页面的y偏移量记录在localStorage中,在页面加载后滚动到该位置。
function storePagePosition() {
var page_y = window.pageYOffset;
localStorage.setItem("page_y", page_y);
}
window.addEventListener("scroll", storePagePosition);
var currentPageY;
try {
currentPageY = localStorage.getItem("page_y");
if (currentPageY === undefined) {
localStorage.setItem("page_y") = 0;
}
window.scrollTo( 0, currentPageY );
} catch (e) {
// no localStorage available
}
$(document).ready(function() {
var url = window.location.href;
console.log(url);
if( url.indexOf('#') < 0 ) {
window.location.replace(url + "#");
} else {
window.location.replace(url);
}
});
// 这个代码加载的页面末尾带有#。
这个完全正常。漂亮干净的JavaScript代码。
var objDiv = document.getElementById("chatbox");
if ( window.history.replaceState ) {
objDiv.scrollTop = objDiv.scrollHeight;
window.history.replaceState( null, null, window.location.href );
}
你应该能够做到。
在页面加载时,检查window.location.hash
是否有值。如果有,获取与哈希值匹配的id元素。找到元素的位置(递归调用offsetTop/offsetLeft)然后将这些值传递给window.scrollTo(x, y)
方法。
这样可以滚动页面到所需的元素。