JQuery scroll to bind with exception

friends

I create a single page site that uses the jQuery function to scroll to the anchor when selecting a menu link. Here is the code I'm using:

(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 elements 'a' with 'href'. I need to change the function above, so it will work for all specific links except this, with a binding #nav-menu:

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

Any suggestions would be much appreciated.

+4
source share
1 answer

Jquery provides a set of built-in filters that you can use in your case:

  • the built-in not () filter is as follows: -

    $("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);
    

0

Source: https://habr.com/ru/post/1665488/


All Articles