首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >vue.js(2) window.scrollY总是返回0

vue.js(2) window.scrollY总是返回0
EN

Stack Overflow用户
提问于 2020-12-01 10:26:00
回答 1查看 492关注 0票数 0

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

  1. window.addEventListener('scroll',...)在我的component.
  2. When中也没有检测到--我在console.log中输入了'window.scrollY‘。
  3. 向右滚动(Y)可用,当客户端移动滚动到底部
  4. 时,无法检测到window.innerHeight不等于0
  5. ,我使用vuestic和vue路由器感谢

代码语言:javascript
复制
  created () {
    // Not working because window.scrollY always return 0
    window.addEventListener('scroll', this.handleScroll);
  },
  methods: {
    handleScroll (event) {}
  }
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2020-12-01 10:58:36

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

并使用getBoundClientRect:

代码语言:javascript
复制
<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>

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

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/65088719

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档