Watch window.scrollY changes in Vuejs

前端 未结 4 1714
野趣味
野趣味 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:02

    window properties can't be used reactively like that. Instead, you'd have to listen to the window's scroll event and respond accordingly:

    mounted() {
      window.addEventListener("scroll", this.onScroll)
    },
    beforeDestroy() {
      window.removeEventListener("scroll", this.onScroll)
    },
    methods: {
      onScroll(e) {
        this.windowTop = window.top.scrollY /* or: e.target.documentElement.scrollTop */
      }
    }
    

提交回复
热议问题