Watch window.scrollY changes in Vuejs

前端 未结 4 1698
野趣味
野趣味 2021-02-15 17:42

I have a very simple app, that has 2 components: the App.vue and another component, Home.vue where I hold the rest of the structure of the app: a stick

4条回答
  •  醉梦人生
    2021-02-15 18:11

    You can use the mounted() method to add your event listener to the window object like this:

    var p = new Vue({
       el: "#app",
       data(){
          return {
              windowTop: 0
          };
       },
       mounted()
       {
           var that = this;
           window.addEventListener("scroll", function(){
               that.windowTop = window.scrollY;
           });
       },
       template: "
    {{windowTop}}
    " });
    
    

提交回复
热议问题