javascript - 使用 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)
在此先感谢您的帮助
解决方案
推荐阅读
- php - symfony 中的表单未使用导航选项卡正确提交
- arrays - 访问矩阵的第 n 个元素
- javascript - 检查 selectpicker 选项中是否存在值
- assembly - 如何比较 2 个 XMM 寄存器 (SSE) 并测试是否相等以跳出循环?
- c - C - 二进制字符加倍
- r - 如果数据框中存在列,则R在列上应用函数
- android - 如何修复Flowable中的subscribeOn(io.reactivex.Schedular)无法应用于(rx.scheduler)
- django - Django ListView 动态上下文数据
- python - 在python中的字符串字符之间添加空格 在python中使用“for”循环
- c# - JWT 令牌过期在 Asp.Net Core API 中不起作用?