简体   繁体   English

如何从页面 URL 中删除哈希值并在重新加载时重置窗口的偏移量

[英]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.

相关问题 链接从url中删除`#`而不重新加载页面 - link to remove `#` from url without reloading the page 如何在网址中最后一个斜杠后删除内容而不重新加载页面? - How to remove content after last slash in the url withouth reloading the page? 从URL删除哈希,将页面加载到无哈希URL,然后在不重新加载页面的情况下将哈希添加到URL - Remove hash from URL, load the page at hash-less URL, then add hash to URL without reloading the page 如何在不重新加载页面的情况下重置事件? - How to reset event without reloading page? 如何删除除第一个以外的所有URL哈希? - How to remove all URL hashes except first? 如何在不重新加载页面的情况下更改页面 url - How to change the page url without reloading the page 如何从使用javascript的window.print()函数打印的页面中删除url文本 - How to remove url text from page printed using javascript's window.print() function 如何在不刷新页面的情况下使用 JavaScript 从 window.location (URL) 中删除散列? - How to remove the hash from window.location (URL) with JavaScript without page refresh? 使用JS Window.Location重写URL而不重新加载页面? - Rewrite URL using JS Window.Location without reloading the page? 如何避免页面重新加载? - How to avoid page from reloading?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM