使用Javascript防止刷新“跳转” [英] Prevent Refresh 'Jump' with Javascript

查看:86
本文介绍了使用Javascript防止刷新“跳转”的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我注意到,如果你在一个页面上并且你向下滚动了一下,如果你刷新页面,大多数浏览器会让你跳回到你的位置。有什么方法可以防止这种情况吗?

I've noticed that if you are on a page and you have scrolled down a good bit, if you refresh the page, most browsers will jump you back down to your position. Is there any way to prevent this?

我看过两个选项,两者都没有在Webkit / Firefox中保持一致。

I have looked at two options and neither are consistent across Webkit / Firefox.

    window.scrollTo(0, 1);
    $('html, body').animate({ scrollTop: 0 }, 0);

任何想法?

您可以查看谷歌搜索结果的示例。

You can check a google search result for an example.

推荐答案

我认为没有理由为什么这不适用于所有浏览器,似乎对我有效(只有一个window.onload功能,使用更多,可能会出现问题)?

I see no reason why this shouldn't work in all browsers, seems to work for me (with only one function for window.onload, use more and there could be problems) ?

window.onload = function() {
  scrollTo(0,0);
}

要点击后退按钮使其工作,也许是这样的:

To make it work when back button is clicked aswell, maybe something like this:

<body onunload="">
<script type="text/javascript">
    window.onload = function() {
       scrollTo(0,0);
    }
</script>
//content here
</body>

这篇关于使用Javascript防止刷新“跳转”的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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