[Vuejs]-Only scroll to top of page on certain pages of my Vue.js router - keep scroll position same on other

1👍

I have –instead of messing with the scrollbehaviour- added to my Home view component the following:

export default {
[..]
  activated() {
    window.scrollTo(0, this.scrollposition);
    console.log("scrolling to this.scrollposition = " + this.scrollposition);
  },
  beforeRouteLeave(to, from, next) {
    this.scrollposition = window.pageYOffset;
    next();
  }
};

This saves the position when you leave to another page, and puts the scrollposition back to what it was when the view gets activated again.

On the case study pages themselves, I have added:

  activated() {
    window.scrollTo(0, 0);
  }

… in order to scroll these to the top when they are ‘opened’.

Seems to work flawlessly, cross-platform and in all modern browsers.

Good enough for me. Hope someone else has some use out of this. 🙂

Leave a comment