75
73

More than 5 years have passed since last update.

Vue.jsでスクロールを検知する

Posted at

コード

index.html
<div id="app">
  <div class="content">
    <span>{{ scrollY }}</span>
  </div>
</div>
main.js
new Vue({
  el: '#app',
  data: {
    scrollY: 0
  },
  mounted() {
    window.addEventListener('scroll', this.handleScroll);
  },
  methods: {
    handleScroll() {
        this.scrollY = window.scrollY;
    }
  }
})

サンプル:
https://jsfiddle.net/z11fe07p/1088/

解説

window.addEventListener('scroll', this.handleScroll);

ここでイベントをリッスンさせて、

handleScroll() {
  this.scrollY = window.scrollY;
}

この関数を呼び出すだけ。

75
73
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
75
73