LoginSignup
0
1

More than 3 years have passed since last update.

10秒で解決!VueRouterで遷移するときにスクロール位置が固定されてしまった時の話

Posted at

今回は、VueRouterで別のページに遷移するときに、遷移する前のスクロール位置によって遷移先のページでトップの位置にならないという謎の事象が発生したので、その解決方法をご紹介します。

以下のコードをrouter/index.jsに追加するだけ。

const router = new VueRouter({
  mode: "history",
  base: process.env.BASE_URL,
  routes,
  //以下のコードを追加
  scrollBehavior(to, from, savedPosition) {
    if (savedPosition) {
      return savedPosition;
    } else {
      return { x: 0, y: 0 };
    }
  },
});

これを解決するのに、めちゃめちゃ時間がかかったので皆さんの助けになればなと思います。

0
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
1