vue.js(2) window.scrollY 总是 return 0

vue.js(2) window.scrollY always return 0

我有一些关于 vuejs 和路由器的问题..

  1. window.addEventListener('scroll', ...) 也未在我的组件中检测到。
  2. 当我在 console.log 中输入 'window.scrollY' 时。它总是 return 0 返回给我。
  3. 右滚动(Y)可用且window.innerHeight不等于0
  4. 当客户端移动到底部时我无法检测到
  5. 我使用 vuestic 和 vue-router 谢谢
  created () {
    // Not working because window.scrollY always return 0
    window.addEventListener('scroll', this.handleScroll);
  },
  methods: {
    handleScroll (event) {}
  }

您可以尝试监听滚动子元素。

并使用 getBoundingClientRect:

<template>
  <div id="app">
    <nav>navbar</nav>
    <main id="listen">main</main>
  </div>
</template>

<script>
export default {
  name: "App",
  created() {
    document.addEventListener("scroll", this.listenScroll);
  },
  destroyed() { // remember to remove the listener when destroy the components
    document.removeEventListener("scroll", this.listenScroll);
  },
  methods: {
    listenScroll() {
      let myScroll = document.querySelector("#listen").getBoundingClientRect()
        .top;
      console.log(myScroll);
    },
  },
};
</script>

<style>
nav {
  height: 100px;
}
main {
  height: 700px;
}
</style>

这里有一个codesandboxhttps://codesandbox.io/s/great-hill-x3wb1?file=/src/App.vue:0-560