Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

203
Views
Vue Router horizontal scroll to hash not working

I want to use a button wrapped into router-link to scroll the viewport horizontally (100% to the right to a vue component. This works fine with anchor tags:

<a href="#about">
    <button class="btn">About</button>
</a>

but as soon as I try to replace them with router-link the horizontal scroll stops working (vertical scroll still works):

<router-link :to="{ hash: 'about' }">
    <button class="btn">About</button>
</router-link>

This function inside router.js is responsible for the scroll:

import Vue from "vue";
import VueRouter from "vue-router";

Vue.use(VueRouter);

export default new VueRouter({
    mode: "history",
    routes: [...],
    scrollBehavior(to, from, savedPosition) {
        if (to.hash) {
            return window.scrollTo({top: document.querySelector(to.hash).offsetTop, behavior: 'smooth'});
        }
        return window.scrollTo({top: 0, behavior: 'smooth'});
    }
});

The component to which the viewport is to be scrolled has been placed on the right side outside the viewport like so:

.about {
  position: absolute;
  transform: translateX(100%);
  height: 100vh;
  width: 100%;
}

Edit:

Changing the button to:

<button @click="$router.push({ hash: 'about' })">About</button>

makes the viewport scroll down about 20% and only vertical , not horizontal. I don't know why this behaviour occurs. scrollBehavior(to, from, savedPosition) {console.log(to)} shows this:

{name: 'home', meta: {…}, path: '/', hash: '#about', query: {…}, …}
fullPath: "/#about"
hash: "#about"
matched: [{…}]
meta: {}
name: "home"
params: {}
path: "/"
query: {}
[[Prototype]]: Object
about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

Try with:

<button @click="$router.push({ hash: 'about' })">About</button>
about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!