LoginSignup
1
0

More than 5 years have passed since last update.

vue.js スクロール位置を指定して router push

Posted at

まずは router.push。
この時、現在のスクロール位置も同時に渡す。


//console.log("現在のスクロール位置" + window.scrollY);
this.$router.push({
    path: "/mugen?page=" + page + "&y=" + window.scrollY,
});

で、
・ハッシュタグがあればハッシュタグへ飛ばす
・移動先に y があれば y の位置に飛ばす

app.js

const router = new VueRouter({
    mode:'history',
    routes,
    scrollBehavior (to, from, savedPosition) {

        // <a v-touch="$root.linkTo('#saikabu')">最下部へ</a>
        if (to.hash) {
            return {selector: to.hash}
        }

        if (savedPosition) {
            return savedPosition;
        } else {

            if(to.query.y){
                return { x: 0, y: to.query.y }
            } else {
                return { x: 0, y: 0 }
            }

        }
    }
});


って感じ。
meta とかに渡してみたが、うんともすんとも行かなかったので。。。

1
0
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
1
0