【问题标题】:Top anchor with smooth scroll that won't repeat具有平滑滚动且不会重复的顶部锚点
【发布时间】:2017-05-27 17:28:02
【问题描述】:
 $(document).ready(function(){
  // Add smooth scrolling to all links
  $("a").on('click', function(event) {

    // Make sure this.hash has a value before overriding default behavior
    if (this.hash !== "") {
      // Prevent default anchor click behavior
      event.preventDefault();

      // Store hash
      var hash = this.hash;

      // Using jQuery's animate() method to add smooth page scroll
      // The optional number (800) specifies the number of milliseconds it takes to scroll to the specified area
      $('html, body').animate({
        scrollTop: $(hash).offset().top
      }, 1000, function(){

        // Add hash (#) to URL when done scrolling (default click behavior)
        window.location.hash = none;
      });
    } // End if
  });
});

我正在运行它以平滑滚动到“#top”而不更改 url-hash。我无意中发现了一种修改它的方法,这样 URL 就不会更新(为“window.location.hash”添加了“none”),但是有一个问题;现在函数不会重复了。

会发生以下情况:

  • 加载页面
  • 向下滚动
  • 按顶锚
  • 页面滚动到顶部(不更新 url)
  • 再次向下滚动
  • 按顶锚
  • 什么都没有发生

不知道我从哪里得到这个 sn-p,但我已经在我的 js 文件夹中有一段时间了。

【问题讨论】:

  • window.location.hash = none; - 如果您查看浏览器控制台,您会看到“none 未定义”。如果您希望在单击 <a href="#">top</a> 链接时防止 URL 出现空哈希,请将 id="top" 添加到 body 或您的第一个/顶部元素或其他任何内容,然后将该链接更改为 <a href="#top">top</a>

标签: javascript jquery html css


【解决方案1】:

您混合了两种滚动页面的方式。

hash 是 URL 中的 #something...对于在页面加载时滚动到锚点很有用。

在此链接中查看#hello 效果:https://codepen.io/Bes7weB/pen/ybWNbJ?editors=1111#hello

然后,为了让用户滚动到页面顶部,我会在触发滚动功能的链接的href 中使用顶部锚点的id
这将触发此特定链接上的功能,而不是页面中的所有链接。 ;)

最后,这一行:window.location.hash = none; 正在尝试将原始 onload 哈希值更改为 none,这被解释为一个未定义的变量,因为它需要一个字符串。

ReferenceError: none 未定义

我会简单地删除该行...
或者如果你真的想清除哈希值:

window.location.hash = "";


工作滚动顶部功能:查看 codePen 以了解 HTML 标记已更改)

 $(document).ready(function(){
  // Add smooth scrolling to all links
  $("a#backTOtop").on('click', function(event) {
    console.log($(this).attr("href"));

    // Make sure this.hash has a value before overriding default behavior
    if ($(this).href !== "") {
      // Prevent default anchor click behavior
      event.preventDefault();

      // Store hash
      var top = $(this).attr("href");

      // Using jQuery's animate() method to add smooth page scroll
      // The optional number (800) specifies the number of milliseconds it takes to scroll to the specified area
      $('html, body').animate({
        scrollTop: $(top).offset().top
      }, 1000, function(){

        // Add hash (#) to URL when done scrolling (default click behavior)
        // window.location.hash = none;
      });
    } // End if
  });
});

【讨论】:

  • 谢谢你!很好的解释:)
【解决方案2】:

如果您不需要影响 url 哈希,只需删除(或用 // 注释)该行代码;因为它会在 javascript 中生成错误。

如果您想从 url 中删除哈希,请尝试以下代码:

history.pushState('', document.title, window.location.pathname + window.location.search);

【讨论】:

  • 我不明白它有什么用处。只是没有关于哈希的代码就可以了。您的建议是将哈希值 A 替换为值 A。没用。
猜你喜欢
  • 2013-08-14
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-05-05
  • 1970-01-01
相关资源
最近更新 更多