jQuery 滚动到锚点异常

jQuery scroll to anchor with exception

朋友,

我正在构建单页网站,该网站使用jQuery函数在选择菜单link时滚动到锚点。这是我使用的代码:

(function($) {
    var jump = function(e)
    {
        if (e) {
            e.preventDefault();
            var target = $(this).attr("href");
        } else {
            var target = location.hash;
        }
        $('html,body').animate(
        {
            scrollTop: $(target).offset().top - 150
        }, 1500, 'swing', function()
        {
            location.hash = target - 150;
        });
    }
    $('html, body').hide()
    $(document).ready(function()
    {
        $('a[href^=#]').bind("click", jump);
        if (location.hash) {
            setTimeout(function() {
                $('html, body').scrollTop(0).show()
                jump()
            }, 0);
        } else {
            $('html, body').show()
        }
    });
})(jQuery)

现在为所有 html 'a' 个具有 'href' 的元素调用此函数。我需要修改上面的函数,所以它适用于所有定义的 links 除了这个带有锚点 #nav-menu:

 <a href="#nav-menu" id="toggle"><span></span></a>

如有任何建议,我们将不胜感激。

Jquery 提供一组内置过滤器,您可以根据自己的情况使用这些过滤器:

  • 内置过滤器not()如下:-

    $("a[href^=#]:not([href=#nav-menu])").click(jump);
    
  • 按如下方式构建您自己的业务过滤器:-

    $("a[href^=#]").filter(function() {
        //you may here do whatever filteration business you want
        return $(this).attr("href")!='#nav-menu';
    }).click(jump);
    

简单示例Here