I'm currently working on a site based on Vue and using Vue Router for routing. My way to handle scroll behavior to anchor links is the following:
const router = new VueRouter({
routes,
scrollBehavior (to) {
if (to.hash) {
return {
selector: to.hash,
offset: { x: 0, y: 140 }
}
}
return { x: 0, y: 0 }
}
})
And my links are built in the following way:
<router-link class="insurance-link d-block" to="#hogar">
<img class="insurance-logo img-fluid" src="@/assets/home-icon.svg">
Hogar
</router-link>
If I click on the link for the first time it scrolls to the anchor position fine, but if I scroll back to the top and click on it for the second time it won't scroll back to that position again. If I click on another anchor link and then click on the previous one everything works fine. Any ideas on what might be happening?
Given I had limited time (and will, to be honest) to keep on trying solutions I opted to go with vue-anchor-router-link (https://github.com/mperator/vue-anchor-router-link), which worked like a charm. Thank you so much to everyone, your answers helped me understand better the way Vue and Vue Router handle anchor links.