LoginSignup
0
0

More than 3 years have passed since last update.

Vue.jsでtopへ戻る機能を実装

Last updated at Posted at 2021-04-11

Vue.jsで実装した機能を備忘録として記録する。
※Vue-CLIを使用しています。

template
<div class="footer">
   <i class="icon-up-open-big scrollToTop" @click="scrollTop"></i>
</div>


js
export default {

  data() {
      return {
          scrollY: 0
      }
  },

  mounted() {
    window.addEventListener('scroll', this.handleScroll);
  },

  methods: {
    handleScroll() {
        this.scrollY = window.scrollY;
        let scrollToTop = document.getElementsByClassName("scrollToTop")[0];

        if(scrollY > 100) {
            scrollToTop.style.display = "inline";
        } else {
            scrollToTop.style.display = "none";
        }
    },

    scrollTop() {
      window.scrollTo({
        top: 0,
        behavior: "smooth"
    });
  }
  }
}

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0