You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Nuxt3中使用nuxt-link时滚动行为失效问题求助

Nuxt3中nuxt-link跳转后页面无法自动回到顶部的问题解决

在Nuxt3项目里使用nuxt-link跳转页面时,遇到滚动行为异常问题:跳转后的新页面会保持和原页面一致的滚动位置,但需求是跳转后页面自动回到顶部。

我已配置/plugins/route.scrollbehaviour.js插件处理路由滚动行为,代码如下:

import { defineNuxtPlugin } from '#app'
export default defineNuxtPlugin((nuxtApp) => {
  nuxtApp.$router.options.scrollBehavior = (to, from, savedPosition) => {
    if (savedPosition) {
      return savedPosition
    } else {
      return { top: 0 }
    }
  }
})

但该配置并未生效,排查后发现问题根源在layouts/default.vue中的CSS样式:

<style>
html, body {
  max-width: 100%;
  overflow-x: hidden;
}
</style>

将这段CSS替换为以下代码后,滚动行为恢复正常,跳转页面后会自动回到顶部:

<style>
html, body {
  max-width: 100%;
  overflow-x: clip;
}
</style>

附上项目package.json内容供参考排查:

{
  "devDependencies": {
    "@nuxtjs/tailwindcss": "^5.3.0",
    "nuxt": "3.0.0-rc.6",
    "nuxt-lodash": "^2.2.0",
    "nuxt-schema-org": "^0.6.2",
    "unplugin-vue-components": "^0.21.1"
  },
  "dependencies": {
    "@ant-design/icons-vue": "^6.1.0",
    "@mdi/js": "^6.7.96",
    "@nuxtjs/device": "Redemption198/device-module",
    "@pinia/nuxt": "^0.3.0",
    "@tailwindcss/typography": "^0.5.2",
    "ant-design-vue": "^3.2.10",
    "date-fns": "^2.28.0",
    "mdi-vue": "^3.0.13",
    "pinia": "^2.0.16",
    "primeicons": "^5.0.0",
    "primevue": "^3.15.0",
    "vant": "^3.5.2",
    "vue-uuid": "^3.0.0",
    "vue3-carousel": "^0.1.40",
    "vue3-google-login": "^2.0.11"
  }
}

内容的提问来源于stack exchange,提问作者imthegrv

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.25 08:54:20