vue on page link or anchor

// scroll to element using quasar library

// uses example; scrollToElement(this.$route.hash)
// uses example; scrollToElement('#cafe-menu')


import { scroll } from 'quasar'
const { getScrollTarget, setScrollPosition } = scroll

    scrollToElement (id) {
      if (id && id !== '') {
        let el = document.querySelector(id)
        if (el) {
          const target = getScrollTarget(el)
          const offset = el.offsetTop
          const duration = 1000
          setScrollPosition(target, offset, duration)
        }
      }
    },

4
1
Steven Lowes 105 points

                                    //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"> </div>




//Your code:
methods: {
  scrollMeTo(refName) {
    var element = this.$refs[refName];
    var top = element.offsetTop;

    window.scrollTo(0, top);
  }
}

4 (1 Votes)
0
5
2
GERSHWiiN 140 points

                                    
// uses example; scrollToElement(this.$route.hash)
// uses example; scrollToElement('#cafe-menu')


scrollToElement (id) {
  // takes input id with hash
  // eg. #cafe-menu
  const el = document.querySelector(id)
  el && el.scrollIntoView({ behavior: "smooth", block: "nearest", inline: "nearest" })

}

5 (2 Votes)
0
Are there any code examples left?
Made with love
This website uses cookies to make IQCode work for you. By using this site, you agree to our cookie policy

Welcome Back!

Sign up to unlock all of IQCode features:
  • Test your skills and track progress
  • Engage in comprehensive interactive courses
  • Commit to daily skill-enhancing challenges
  • Solve practical, real-world issues
  • Share your insights and learnings
Create an account
Sign in
Recover lost password
Or log in with

Create a Free Account

Sign up to unlock all of IQCode features:
  • Test your skills and track progress
  • Engage in comprehensive interactive courses
  • Commit to daily skill-enhancing challenges
  • Solve practical, real-world issues
  • Share your insights and learnings
Create an account
Sign up
Or sign up with
By signing up, you agree to the Terms and Conditions and Privacy Policy. You also agree to receive product-related marketing emails from IQCode, which you can unsubscribe from at any time.
Creating a new code example
Code snippet title
Source