【问题标题】:Adjust position of anchor destinations using Jquery to take into account sticky header使用 Jquery 调整锚点的位置以考虑粘性标题
【发布时间】:2013-12-22 05:39:50
【问题描述】:

我正在尝试在使用锚点时调整滚动的位置,因为我使用的是粘性标题,并且锚点位置似乎基于顶部,即使我在页面本身上工作的主 div 上有 css 填充但是当我点击一个锚点时它不能正常工作。

我输入了这个平滑的滚动代码,只要锚链接来自页面本身,它就可以正常工作,但是当我从另一个页面直接导航到锚时它没有任何效果

$('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 - 120)
            }, 1000);
            return false;
        }
    }
});

任何人都可以帮我完成这项工作,因此无论点击是来自另一个页面还是来自锚点所在的页面内,都是一样的。

【问题讨论】:

    标签: jquery scroll


    【解决方案1】:

    我最终使用了在 a similar question 中找到的 CSS。

    a[name] {
        position: relative;
        top: -160px;
        height: 1px;
        display: block;
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-07-31
      • 2018-10-27
      • 2016-11-04
      相关资源
      最近更新 更多