Closed atiar-cse closed 4 years ago
I think the correct approach should be to call this.$smoothScroll programmatically when entering a new page component through the route to scroll the page to the specified DOM node
I added smooth scroll in the mounted section of my home page like so:
let hash = this.$route.hash; if (hash) { hash = hash.replace("#", "") let section = window.document.getElementById(hash) this.$nextTick(() => { this.$smoothScroll({ scrollTo: section, duration: 500, }) }) }
However, this scrolls untill it sees the relevant section. When I use it in a directive manner, it works correctly. I.e. the section is scrolled to the top of the screen. I have to use smooth scroll progamatically, because I am coming from a different page just like OP.
The codes are working but the problem is, when I have the href like
<a href="/#anchor">Anchor</a>
, it does not seem to work with my codes. If I have the same without the forward slash, it works like<a href="#anchor">Anchor</a>
. When visit new route (/contact-us) - it loaded asbase_url/contact-us#anchor
and never works because the #anchor section inside home page.I am using vue-router.
Any help will be appreciated.