[英]How to remove the hashes from the page URL and reset the offset of the window on reloading
i'm having a problem with the anchor elements of html.我对 html 的锚元素有问题。 When i reload the page, the URL continues with the hash of the anchor element on it and the window offset doesn't go back to the top.当我重新加载页面时,URL 继续使用锚元素的哈希值,并且窗口偏移量不会回到顶部。 I think the browser understands that it's a new page and thus doesn't reload to the original offset position of the window.我认为浏览器知道这是一个新页面,因此不会重新加载到窗口的原始偏移位置。 That's my code:那是我的代码:
<nav class="desktopMenu">
<ul>
<a href="#home">
<li>Home</li>
</a>
<a href="#about">
<li>About</li>
</a>
<a href="#abilities">
<li>Abilities</li>
</a>
<a href="#gameplay">
<li>Gameplay</li>
</a>
</ul>
</nav>
So, i've tried to clean the hash from the URL with this:因此,我尝试使用以下方法清除 URL 中的哈希值:
window.location.hash = '';
$(window).offset().top = 0;
It cleans the what comes after the hash but doesn't reset the window offset.它会清除散列之后的内容,但不会重置窗口偏移量。
You can scroll to top of the Page by:您可以通过以下方式滚动到页面顶部:
$([document.documentElement, document.body]).animate({
scrollTop: 0
}, 0);
an Example:一个例子:
https://jsfiddle.net/praveen_tamil/s0qu8n2t/2/ https://jsfiddle.net/praveen_tamil/s0qu8n2t/2/
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.