通过浏览器的后退按钮访问时如何将 jQuery 动画页面恢复到初始状态?
我正在制作的网站有一些基本的 jQuery 动画效果,包括当每个新页面打开时从浏览器窗口下边缘下方滑动主要内容的动画,以及将相同内容向上滑出浏览器窗口的动画单击任何菜单链接时,在打开新页面之前。
当我使用浏览器的“后退”按钮返回到上一页时,主要内容仍然隐藏(因为页面上发生的最后一件事是将内容从浏览器窗口中删除的动画)。
问题:当通过单击浏览器的后退按钮访问页面时,如何将页面刷新/重置为初始状态?
我认为这可以通过在通过浏览器后退按钮访问页面时强制刷新页面来实现 - 尽管我可能是错的。我非常感谢对此的建议!
(这个话题之前已经提出过,但不知何故,我在这个网站上找到的答案都没有完全解决这个问题。)
The website I'm making has a few basic jQuery-animated effects, including the animation that slides the main content from below the lower edge of the browser window when every new page opens, and slides the same content up and out of the browser window when any menu link is clicked, before opening the new page.
When I go back to the previous page using the browser's "back" button, the main content remains hidden (because the last thing that happen to the page was the animation that removed the content out of the browser window).
Question: how can I refresh / reset the page to its initial state when it's visited by clicking the browser's back button?
I assume this can be achieved by forcing a page refresh when it's visited via browser back button - even though I may be wrong. I would greatly appreciate advice on this!
(This topic has been brought up before but somehow none of the answers I found on this website quite solves the issue.)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
Shawn Chin 找到了解决这个问题的简单而优雅的方法。请参阅此讨论:浏览器“后退”按钮与 jQuery 动画页面
The simple and elegant solution of this problem was found by Shawn Chin. Please see this discussion: Browser "Back" Button vs. jQuery-animated page
对于初学者来说,您的网站似乎在 Chrome 中运行良好(没有隐藏内容)。但是,当我在 Firefox 中加载页面时,我看到了您所描述的行为。
但是,要强制页面“刷新”,您可以首先阻止其缓存。尝试将类似的内容添加到
部分。
实际上,这些标头应该从服务器推送。请参阅此问题或此问题。
或者,您可以在页面加载时检查隐藏 div 的位置(在
$(document).ready()
部分)。如果它不在应有的位置,请使用location.reload();
重新加载页面。对于您的网站,类似这样的操作应该可行。
请注意,您的页面上只能有一个
window.onload
或$(document).ready
。因此,如果您已经有一个,只需将if
语句放入其中即可。For starters, your site seems to work fine in Chrome (no hidden content). However, I see the behavior you describe when I load the page in Firefox.
However, to force your page to 'refresh', you can prevent it from caching in the first place. Try adding something like this to the
<head>
section.Really, these headers should be pushed from the server. See this question or this question.
Alternatively, you can check the position of the hidden div when the page loads (in the
$(document).ready()
section). If it's not where it's supposed to be, reload the page usinglocation.reload();
For your site, something like this should work.
Please note that you should only have ONE
window.onload
or$(document).ready
on your page. So if you already have one, simply place theif
statement in there.