函数不起作用,我的 sintax 失败了吗?

Function doesn't work, have i sintax fail?

好吧,我把我的 JS 文件放在其中,其中包含有问题的代码:

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

});

前两个效果很好,但第三个效果不好。事实上,我的控制台显示了这个错误:

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

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

那么,错在哪里呢?谢谢!

您应该在第 13 行中使用逗号而不是冒号

.animate() 方法语法为:

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

在哪里

  • properties 是一个具有 CSS 属性和值的普通对象,动画将向其移动。
  • duration 是数字或字符串类型,确定动画将持续多长时间 运行。

所以,只需像这样更新您的代码:

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