美文网首页
Vue监听单个组件滚动

Vue监听单个组件滚动

作者: 旸云 | 来源:发表于2018-10-26 21:42 被阅读0次

首先我们要在需要监听的组件上加一个$ref

<div class="scrollComponentContainer" ref="viewBox">
  ...
</div>

然后在methods里加一个监听的方法

scrollListener:function(){
            this.box = this.$refs.viewBox
            this.box.addEventListener('scroll', () => {
                colsole.log(this.$refs.viewBox.scrollTop)
            }, false)
        },

最后在钩子函数mounted里运行这个方法

mounted(){
  this.scrollListener()
}

齐活

相关文章

网友评论

      本文标题:Vue监听单个组件滚动

      本文链接:https://www.haomeiwen.com/subject/rqzltqtx.html