【问题标题】:Remove anchor link after scrolling - works also on links from another page滚动后删除锚链接 - 也适用于其他页面的链接
【发布时间】:2019-10-12 21:44:41
【问题描述】:

我已经建立了一个平滑滚动的单页网站,在平滑滚动后从 URL 中去除锚链接。这是我正在使用的 jQuery:

$(function() {
    $('a.page-scroll').on('click', function(event) {
        var $anchor = $(this);
        $('html, body').stop().animate({
            scrollTop: $($anchor.attr('href')).offset().top  - 60
        }, 1000, 'easeInOutExpo');
        event.preventDefault();
    });
});

在我添加其他页面之前,一切都很好。在另一个外部页面上的<a class="page-scroll" href="../#contact">Contact</a> 之类的链接之后,我无法删除锚链接。

我在 SO 上进行了高低搜索,但找不到有效的解决方案。

如果链接来自外部页面,我并不完全关心平滑滚动。我最需要的是导航/滚动到主页的 id 部分(带有偏移以适应固定导航)并在链接来自外部页面(来自我网站上的其他页面)时从浏览器 URL 窗口中删除锚链接,或来自其他网站)。

我也试过这个,但它同样只适用于同一页面上的 id 的内部链接:

<script>
$(function() {
  $('a[href*=#]:not([href=#])').click(function() {
    if (location.pathname.replace(/^\//,'') == this.pathname.replace(/^\//,'') && location.hostname == this.hostname) {

      var target = $(this.hash);
      target = target.length ? target : $('[name=' + this.hash.slice(1) +']');
      if (target.length) {
        $('html,body').animate({
          scrollTop: target.offset().top - 60
        }, 1000);
        return false;
      }
    }
  });
});
</script>

我也试过这个,但没有运气:

    <script type="text/javascript">
(function($) {
    $(document).ready(function() {
    var url=document.URL.split("#");
    var ancher=url[1];
         $('html, body').animate({
           'scrollTop':   $('#'+ancher).offset().top - 60
         }, 5000);
    });
})(jQuery);
  </script>

任何新年前夜的帮助都将不胜感激,这样我就可以完成这个项目!

【问题讨论】:

  • 您是否知道 jQuery 的 bind 方法自 1.7 版以来已被弃用?
  • 感谢鲁布斯托。剧本来自另一个人写的剧本。我需要自己写这些东西。都是好时光。感谢您的帮助。
  • 让它在a solution Ian Clark 的基础上工作 - 谢谢!

标签: javascript jquery


【解决方案1】:

我可能不了解问题的范围,但我相信您正在努力做到这一点,因此 href 不会在想要滚动的页面上触发,但会在链接到其他页面的页面上触发,并且不是页面本身的部分。也许这样的事情对你有用:

$(function() {
    $('a.page-scroll').bind('click', function(event) {
        var $anchor = $(this);
        if ($anchor[0].href[0] === '#') {
            $('html, body').stop().animate({
                scrollTop: $($anchor.attr('href')).offset().top  - 60
            }, 1000, 'easeInOutExpo');
            event.preventDefault();
            return false;
        } else {
            return true;
        }
    });
});

这样做的目的是查看 href 中的前导字符是一个 #,这意味着它是一个指向其自身部分的链接。

让我知道这是否有帮助和/或我是否走在正确的轨道上。

ps:我把 .bind 留在了那里,因为我不知道你使用的是什么版本的 jQuery,但是较新的语法是使用 .on

新年快乐

只是稍微附加一下,以便主页的深层链接转到适当的部分,但没有哈希标签:

您可以从 window.location 中删除该“哈希”变量,但如果您尝试完全删除主题标签,则会导致浏览器刷新。这也会导致观看者失去位置(从而消除深层链接的目的)。

更改井号标签值(保留#):

window.location.hash = ''; // clears the hash tag

要删除井号标签及其值(清除 # 和过去的所有内容):

window.location.href = window.location.href.substr(0, window.location.href.indexOf('#')); // this causes a browser refresh

如果不是很明显,您可以在页面加载时运行它

$(document).ready(function() {
    if (typeof(window.location.hash) !== 'undefined' && window.location.hash.length > 0) {
        window.location.hash = ''; // will clear the hash anytime someone arrives with a hash tag
    }
});

【讨论】:

  • 您好,非常感谢您的帮助,很抱歉没有更清楚。我有两种情况。第一个,使用问题中列出的第一个 jQuerry 函数运行良好,是在主页内平滑滚动,从内部/页面上的链接,并从浏览器窗口中的 URL 中删除锚链接。我需要帮助的第二种情况是对从其他页面到主页的链接执行相同的操作。因此,从mainpage.com/subpagemainpage.com/#contact 的链接将滚动到#contact,但在浏览器窗口中仅显示mainpage.com
  • 我将删除 .bind 并替换为 .on - 也感谢您。
  • 这是the live website 的链接,其中包含我所描述的问题。从项目/投资组合页面的导航中单击返回主页时,我试图从浏览器 URL 中删除 #what-i-do 和 #portfolio 和 #contact 锚链接,同时还为锚设置 60px 偏移弥补固定导航的链接。
  • 哦,那么您要做的是在用户从其他地方到达页面后删除哈希?你会遇到困难。例如,您可以设置 window.location.hash = '',但它不会删除实际的哈希标签。删除实际的哈希标签将导致浏览器刷新页面。
  • 你必须明白,有了真正的 SPA,你永远不会离开主页。内部链接仍然会有一个子页面 url(如 /contact),由 SPA 引擎来删除它(如果你愿意的话)。
【解决方案2】:

对于平滑滚动的页面,请尝试使用replaceState()
它将从浏览器 URL 窗口中删除锚链接处的主题标签(无需重新加载页面)。

// smooth scrolling
function scrollTo(selectors)
{
    if(!$(selectors).length) return;
    var selector_top = $(selectors).offset().top - 0;
    $('html,body').animate({ scrollTop: selector_top }, 'slow');
}    

// do scroll and clear the hash tag    
$(window).on('load', function(){          
    if( typeof(location.hash) !== 'undefined' && location.hash.length ) {
       scrollTo(location.hash);
       history.replaceState(null, null, location.pathname);                      
    }       
});

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-09-08
    • 2021-04-04
    • 1970-01-01
    • 1970-01-01
    • 2016-12-12
    • 1970-01-01
    相关资源
    最近更新 更多