Jquery scroll fixed on top when clicking on a div #

0

Next I have a problem because when I clicked on a menu anchored by a #id it did not have the scroll on top and so the content was cut because now I put this function and it worked perfectly however when I look at the console error

Jquery:

 $('.sub-menu a').click(function() {

    var anchor;
    var link = $(this).attr('href');
    if (link.indexOf('#') !== -1) {
        anchor = '#' + link.split('#').pop();
    }

    $('.id-sub').slideUp(2000);
     if (anchor !== undefined) {
        $(anchor).slideDown(2000).addClass('fixed');
        $('html,body').animate({ 'scrollTop': $(anchor) }, function(){
            window.location.hash = target;
        });
     }

});

This is the error:

Uncaught ReferenceError: target is not defined
at HTMLBodyElement.<anonymous> (scripts.js?ver=4.9.7:30)
at HTMLBodyElement.d.complete (jquery.js?ver=1.12.4:4)
at i (jquery.js?ver=1.12.4:2)
at Object.fireWith [as resolveWith] (jquery.js?ver=1.12.4:2)
at i (jquery.js?ver=1.12.4:4)
at n.fx.tick (jquery.js?ver=1.12.4:4)

But I tried to draw this line:

     function(){
        window.location.hash = target;
    });

I already tried to create a var like null and nothing works the error until it leaves with these solutions above but the scroll is not fixed at the top could you help me?

    
asked by anonymous 19.07.2018 / 16:34

1 answer

0
//Efeito SliderDown/SliderUp
$('.sub-menu a').click(function() {

    var anchor;
    var link = $(this).attr('href');
    if (link.indexOf('#') !== -1) {
        anchor = '#' + link.split('#').pop();
        console.log('anchor',anchor);
    }

    $('.id-sub').slideUp(2000);
    if (anchor !== undefined) {
        $(anchor).slideDown(2000).addClass('fixed');       
        $('html,body').animate({ 'scrollTop': $(anchor) }, 600, function(){
            window.location.hash = target;
        })
    }
});
    
19.07.2018 / 21:06