简体   繁体   English

jQuery滚动到锚点异常

[英]jQuery scroll to anchor with exception

friends, 朋友们,

I'm building single page website, which uses the jQuery function to scroll to an anchor, when the menu link is being selected. 我正在构建单页网站,当选择菜单链接时,它使用jQuery函数滚动到锚点。 Here is the code I use: 这是我使用的代码:

(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)

Now this function is called for all html 'a' elements with 'href'. 现在,所有带有'href'的html'a'元素都会调用此函数。 I need to modify the function above, so it would work for all defined links except this one with the anchor #nav-menu : 我需要修改上面的函数,所以它适用于所有已定义的链接,除了这个带锚#nav-menu

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

Any suggestions would be very appreciated. 任何建议将非常感激。

Jquery provide a set of built-in filters that you can use in your case you may use: Jquery提供了一组内置过滤器,您可以在您使用的情况下使用它们:

  • the built in filter not() as following:- 内置过滤器not()如下: -

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

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

Simple Example Here 这里简单的例子

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM