Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Vue.js anchor to div within the same component

I'm developing a Vue.js application and I'm having trouble to link an anchor to a certain div within a component.

I have the following anchor:

<a href="#porto" class="porto-button">Porto, Portugal</a>

and the following div:

<div id="porto" class="fl-porto">

I'm using vue-router in hash mode.

The problem is, whenever I click the "porto-button" it will redirect me to the "home" page ( ' / ' )

I'm using Vue.js 1.X and I tried using history mode (URL without the hashbang) but it gives me a cannot GET '/page' error upon refreshing a page.

Am I doing something wrong? What can I do about this?

like image 402
fmlopes Avatar asked Mar 07 '17 10:03

fmlopes


3 Answers

Because you are using router in hash mode, you will not be able to scroll that easily because scrolling to /#something will actually redirect you to 'something' page.

You will have to emulate scrolling behaviour on your own, try doing something like that:

//P.S. the code is written for Vue 2.
//You will have to adjust it to Vue 1.

//Your view:
<a class="porto-button" @click="scrollMeTo('porto')">Porto, Portugal</a>
...
<div ref="porto" class="fl-porto">
//Your code:
methods: {
  scrollMeTo(refName) {
    var element = this.$refs[refName];
    var top = element.offsetTop;

    window.scrollTo(0, top);
  }
}

How it works:

  1. Set the references through ref attribute to the element you would like to scroll to;
  2. Write a function that will programmatically set window.scrollY to the top of the referenced element.
  3. Job is done :)

Update 1:

jsfiddle https://jsfiddle.net/5k4ptmqg/4/

Update 2:

Seems that in Vue 1 ref="name" looked like el:name (docs), here is an updated example:

https://jsfiddle.net/5y3pkoyz/2/

like image 68
euvl Avatar answered Nov 16 '22 08:11

euvl


Another method is to use "scrollIntoView()"

So, euvl's code still stands, except you would change the method slightly:


    new Vue({
      el: '#app',
      methods: {
        goto(refName) {
            var element = this.$els[refName];
          element.scrollIntoView();
        }
      }
    })

If you wanted to get fancy and make the scroll smooth, you can even add the following:

element.scrollIntoView({ behavior: 'smooth' });

Note that this will need a polyfill for older browsers.

like image 39
Cathy Ha Avatar answered Nov 16 '22 06:11

Cathy Ha


What worked for me

<router-link to="#leaders">Leaders</router-link>

or dynamic

<router-link :to="`#${subMenuItem.linkTarget}`" class="page-submenu-list__link">
                    {{subMenuItem.linkTitle}}
                </router-link>

in router

routes:[],
scrollBehavior (to, from, savedPosition) {
    //https://router.vuejs.org/guide/advanced/scroll-behavior.html
    if (to.hash) {
            return { selector: to.hash }
        } else if (savedPosition) {
            return savedPosition;
        } else {
            return { x: 0, y: 0 }
        }
  }
like image 11
atazmin Avatar answered Nov 16 '22 07:11

atazmin