首页 > 解决方案 > 内容在第二次单击固定标题上的相同锚链接时更改其位置

问题描述

我正在开发一个带有固定标题的单页网站。我面临的问题是,当我单击导航栏链接时,它会完全转到内容,但是当我再次单击同一链接时。它稍微改变了内容的位置。

https://imgur.com/a/vzXYR1P

JS: var windows = $(window); var 粘性 = $('#sticker');

windows.on('scroll', function() {
    var scroll = windows.scrollTop();
    if (scroll < 400) {
        sticky.removeClass('stick');
    }else{
        sticky.addClass('stick');
    }
});



$('a.pagess:not(:first)').click(function(e){
        e.preventDefault();
        var y = 125;
        $('html ,body').animate({enter code here
            scrollTop: $(this.hash).offset().top - y
        });

});

标签: javascriptjquery

解决方案


没关系,我知道了,实际上我的计算有点错误。所以我稍微更改了代码,现在一切正常。

 $('a.pagess:not(:first)').click(function(e){
        e.preventDefault();
        var y = 125;
        if (windows.scrollTop() > 400 ){
            y = 55;
        }
        $('html,body').animate({
            scrollTop: $(this.hash).offset().top - y
        });

        window.history.pushState(null, null, this.href);

    });

推荐阅读