我怎么能在这个脚本上添加 class

How could I add class on this script

我在我的项目中使用了这个脚本,一个带有滚动条的页面。现在我的问题是每次页面在确切的部分滚动时,标题总是被我的固定菜单隐藏。下面是脚本

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

希望大家能帮帮我。 非常感谢!

You'll need to take into account the height of the fixed header when applying scrollTop

如果我的评论不够清楚,试试这个。

$('a[href*=#]:not([href=#])').click(function() {
// does the pathname of this link match that of the current location.pathname?
var activeLink = (location.pathname.replace(/^\//,'') == this.pathname.replace(/^\//,''));

if (activeLink || location.hostname == this.hostname) {

    var target = $(this.hash);

    target = target.length ? target : $('[name=' + this.hash.slice(1) +']');

    if (target.length) {

         // here we will take into account the height of #navbar
         var offset = target.offset().top + $('#navbar').height();

        $('html,body').animate({
             scrollTop: offset // apply our new offset
        }, 1500,'easeInOutExpo');

        return false;
    }
}

});