nuxt.js - 如何在 nuxt.js 和引导站点中平滑滚动?
问题描述
我尝试使用 nuxt.js 和这个模板制作网站。我将文件夹:css、img、js、scss、vendor 从模板复制到我的 nuxt 项目的静态文件夹。在此之后,我制作了 nuxt.config.js和pages/index.vue文件。除了页面上的平滑滚动外,所有工作都很好。我在浏览器控制台中看到错误:
agency.min.js:7 Uncaught TypeError: Cannot read property 'top' of undefined
at a (agency.min.js:7)
at agency.min.js:7
at agency.min.js:7
我该如何解决这个问题?
UPD:您可以下载此项目以重现错误
解决方案
First, you need to overwrite the router default behavior like this. Paste this anywhere inside your nuxt.config.js:
{
//.......
router: {
scrollBehavior(to) {
if (to.hash) {
return window.scrollTo({
top: document.querySelector(to.hash).offsetTop + window.innerHeight,
behavior: 'smooth'
})
}
return window.scrollTo({ top: 0, behavior: 'smooth' })
}
}
}
Then your anchor link should be like this:
<nuxt-link :to="{ path: '/',hash:'#about'}">Contact</nuxt-link>
推荐阅读
- qt - QML QtCharts CandlestickSeries 中继器
- java - Junit 5 测试 toString()
- java - JHipster OpenAPI 模型包
- sql-server - 在 SQL 中计算聚合选择语句
- php - 如何在 amazon-ecs 中设置 nginx/php 项目?
- json - $request->json()->all() 返回空 Laravel
- c# - 以 ObjectDisposed 异常完成的任务
- ada - 未能断言 libsparkcrypto SHA256 结果相等
- docker - VPN 连接的 Docker 容器无法访问其他容器
- ios - Swift - 如何从 TableView 中的多个选定单元格中将数据保存到 Firebase?