首页 > 解决方案 > 使用 Jquery 构建的平滑滚动功能难以正常运行

问题描述

难以让我的 Jquery 平滑滚动功能工作我不知道为什么它不工作请看下面的代码

  //Jquery Smooth Scroll
$('.navbar a').on('click', function(e) {
if (this.hash !== '') {
    e.preventDefault();

    const hash = this.hash;

    $('html, body').animate({
            scrollTop: $(hash).offset().top
        },
        800
      );
    }
});

但是,当我单击导航栏时,我收到此错误消息:

  Uncaught TypeError: Cannot read property 'top' of undefined
  at HTMLAnchorElement.<anonymous> (main.js:9)
  at HTMLAnchorElement.dispatch (jquery-3.4.1.min.js:2)
  at HTMLAnchorElement.v.handle (jquery-3.4.1.min.js:2)

在此先感谢您的帮助

标签: javascriptjqueryhtmlcss

解决方案


推荐阅读