Vue => Vue监听组件滚动事件

在dom元素上加ref,利用this.$refs.recordwrapper获取到元素,添加滚动监听事件,希望得到的结果是滚动触发事件handleScroll,现在情况是失效,并没有监听到滚动动作,或者说滚动动作并没有出发事件

  • 问题:监听事件的参数写错了,带括号就直接调用了函数,去掉括号就好了
<template>
	<div class="main">
	  <div class="content" ref="recordwrapper">
	  </div>
	</div>
</template>
 
<script>
export default {
  data() {
  },
  watch: {
    'chatList': function(e) {
      if (e.length) {
        this.$nextTick(() => {
          this.$refs.recordwrapper.addEventListener('scroll', this.handleScroll);
        })
      }
    }
  },

  methods: {
       handleScroll () {
      // 变量scrollTop是滚动条滚动时,距离顶部的距离
      this.$nextTick(() => {
        // 获取需要滚动的距离
        var scrollTop = this.$refs.recordwrapper.scrollTop
        // 变量windowHeight是可视区的高度
        var windowHeight = this.$refs.recordwrapper.clientHeight
        // 变量scrollHeight是滚动条的总高度
        var scrollHeight = this.$refs.recordwrapper.scrollHeight
        // 滚动条到底部的条件
        console.log(scrollTop, windowHeight, scrollHeight);
        if (scrollTop == 0 && this.chatList.length !== 0) {
          if (this.count > 1) {
            this.topLoading = true
            this.count -= 1;
            this.getWechatCallDetail()
          }
        }
      })
    }
 
  },
};
</script>
 
<style scoped lang="scss">
.main{
  width: 100%;
  height: 100%;
  padding: 0;
  margin: 0;
  display: flex;
  display: -webkit-flex;
  flex-direction: column;
  -webkit-flex-direction:column;
  .head{
    width: 100%;
    position: fixed;
    top: 0;
    left:0;
    background: #eee;
  }
  .content{
    margin-top: 55px;
    height: 100%;
    flex: 1;
    -webkit-flex: 1;
  }
}
</style>
  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

不停喝水

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值