Search code examples
javascriptjqueryfunctiononclicknav

Function doesn't work, have i sintax fail?


Well, i put my JS file which contains the code what has the problem:

$(document).ready(function () {
$('.menu-toggler').on('click', function () {
    $(this).toggleClass('open');
    $('.top-nav').toggleClass('open');
});

$('.top-nav .nav-link').on('click', function () {
    $('.menu-toggler').removeClass('open');
    $('.top-nav').removeClass('open');
});

$('nav a[href*="#"]').on('click', function () {
    $('html, body').animate( keyframes: {
        scrollTop: $($(this).attr('href')).offset().top - 100
    }, options:2000);
});

});

The first two work nice, but the third doesn't. Indeed, my console shows this error:

',' expected. ts(1005) [13, 43]

',' expected. ts(1005) [15, 19]

So, where is the mistake? Thanks!


Solution

  • The .animate() method syntax is:

    .animate( properties [, duration ] [, easing ] [, complete ] )
    

    Where

    • properties is a plain object of CSS properties and values that the animation will move toward.
    • and duration is of type number or string determining how long the animation will run.

    So, just update your code like:

    $('html, body').animate({
        scrollTop: $($(this).attr('href')).offset().top - 100
    }, 2000);