当前位置: 移动技术网 > IT编程>脚本编程>vue.js > vue scroll滚动判断的实现(是否滚动到底部、滚动方向、滚动节流、获取滚动区域dom元素)

vue scroll滚动判断的实现(是否滚动到底部、滚动方向、滚动节流、获取滚动区域dom元素)

2020年06月23日  | 移动技术网IT编程  | 我要评论

1、是否滚动到底部

 isscrollbottom() {
   // 是否滚动到了底部
   this.box = this.$refs.chatlistwrapper
   var clientheight = this.box.clientheight
   var scrolltop = this.box.scrolltop
   var scrollheight = this.box.scrollheight
   if (scrolltop + clientheight == scrollheight) {
    this.$store.dispatch('setbottombtn', false, { root: true }) // 隐藏直达最新消息按钮
    this.isbottom = true
    this.istop = false
   } else {
    this.$store.dispatch('setbottombtn', true, { root: true }) // 显示直达最新消息按钮
    this.istop = false
    this.isbottom = false
    if (scrolltop == 0) {
     this.istop = true
    }
   }
  },

2、scroll滚动方向判断

  getdirection() {
   // scroll滚动方向~~~~
   this.box = this.$refs.chatlistwrapper
   var scrolltop = this.box.scrolltop
   var scroll = scrolltop - this.inittop
   this.inittop = scrolltop
   let dir = 'down'
   if (scroll < 0) {
    dir = 'up'
   } else {
    dir = 'down'
   }
   return dir
  },

3、滚动节流

1)、在滚动的dom上绑定scroll事件,监听滚动


2)、data中定义:fnscroll: () => {}, 初始值
3)、mounted中给fnscroll函数赋值,_.throttle实现滚动节流

this.fnscroll = _.throttle(() => {
}, 500)

4、获取滚动可视区域内dom:

实现注意:判断当前元素是否在可视区域内,若在则存到isseedomarr中,然后循环isseedomarr数组,拿到当前可视区域内的最后一个dom,再去判断是否更新对应的咨询轨迹。
不要滚动时就去更新,这样会造成不停请求更新,最后一次请求可能无效,造成数据的错乱

  sendread() {
   const chatli = document
    .getelementbyid('chat_list_wrapper')
    .getelementsbytagname('li')
   var container = this.$refs.chatlistwrapper
   var swheight = container.clientheight
   const scrolltop = container.scrolltop
   const aa = swheight + scrolltop
   let isseedomarr = []
   for (let j = 0; j < chatli.length; j++) {
    if (scrolltop < chatli[j].offsettop && chatli[j].offsettop < aa) {
     isseedomarr.push(chatli[j]) //将可视区域内所有dom存储到isseedomarr
    }
   }
   if (isseedomarr.length) {
    // 非 ceo接诊台更新消息的已读状态
    if (this.$route.path.indexof('diagnose/ceo') === -1) {
     for (let m = 0; m < isseedomarr.length; m++) {
      const isselfsend = isseedomarr[m].getattribute('isselfsend')
      const msgstatus = isseedomarr[m].getattribute('msgstatus')
      const msgtype = isseedomarr[m].getattribute('msgtype')
      if (!isselfsend && !msgstatus && msgtype !== 'notice') {
       const _id = isseedomarr[m].getattribute('id')
       this.sendreadapi(_id)
      }
     }
    }
    // 更新聊天对应的咨询轨迹
    this.setcurrentfdask(
     isseedomarr[isseedomarr.length - 1].getattribute('fdask')
    )
   }
  },

the end:滚动加载这些判断前前后后改了好多次,这次终于感觉逻辑比较清晰了,也算对自己有个交代。。。

到此这篇关于vue scroll滚动判断的实现(是否滚动到底部、滚动方向、滚动节流、获取滚动区域dom元素)的文章就介绍到这了,更多相关vue scroll滚动判断内容请搜索移动技术网以前的文章或继续浏览下面的相关文章希望大家以后多多支持移动技术网!

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网