Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

207
Visualizações
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 Respostas
Responde à pergunta

0

Try with:

<button @click="$router.push({ hash: 'about' })">About</button>
about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda