分页,判断元素是否在可视区域内

 // 获取滚动条当前的位置
    getScrollTop () {
      var scrollTop = 0
      if (document.documentElement && document.documentElement.scrollTop) {
        scrollTop = document.documentElement.scrollTop
      } else if (document.body) {
        scrollTop = document.body.scrollTop
      }
      return scrollTop
    },
    // 获取当前可视范围的高度
    getClientHeight () {
      var clientHeight = 0
      if (document.body.clientHeight && document.documentElement.clientHeight) {
        clientHeight = Math.min(document.body.clientHeight, document.documentElement.clientHeight)
      } else {
        clientHeight = Math.max(document.body.clientHeight, document.documentElement.clientHeight)
      }
      return clientHeight
    },
    // 获取文档完整的高度
    getScrollHeight () {
      return Math.max(document.body.scrollHeight, document.documentElement.scrollHeight)
    }

事件

window.addEventListener('scroll', this.scrollFun, true)

获取滚动条在元素内的滚动高度:通过滚动条的高度+设备本身高度-元素距离顶部的高度

((this.getScrollTop() + this.getClientHeight() - scroolCon.offsetTop) < 0 ? 0 : (this.getScrollTop() + this.getClientHeight() - scroolCon.offsetTop))
 scrollFun () {
      debugger
      var scroolCon = this.$refs.scroolCon //内容元素通过Vue的ref属性
      var scroolConScrol = scroolCon.offsetTop + scroolCon.offsetHeight
      console.log('pageYOffset' + window.pageYOffset)
      console.log('offsetTop' + scroolCon.offsetTop) // 值大于0在文档区域中滚动
      console.log('offsetHeight' + scroolCon.offsetHeight) // 值大于0在文档区域中滚动
      console.log('clientTop' + scroolCon.clientTop) // 值大于0在文档区域中滚动
      console.log('scrollHeight' + scroolCon.scrollHeight) // 值大于0在文档区域中滚动
      console.log('scrollTop' + scroolCon.scrollTop) // 值大于0在文档区域中滚动
      console.log('getScrollTop' + this.getScrollTop()) // 值大于0在文档区域中滚动
      var scrConHeight = ((this.getScrollTop() + this.getClientHeight() - scroolCon.offsetTop) < 0 ? 0 : (this.getScrollTop() + this.getClientHeight() - scroolCon.offsetTop))
      console.log('滚动高度' + scrConHeight)
      console.log('距离滚到底部' + (scroolCon.offsetHeight - scrConHeight))
      if (scroolCon.offsetHeight - scrConHeight < 50) {
        console.log('滚到底部')
      }
      if (scroolCon.offsetTop >= this.getScrollTop() && scroolCon.offsetTop < (this.getScrollTop() + this.getClientHeight())) {
        console.log('在可视区域' + ((this.getScrollTop() + this.getClientHeight() - scroolCon.offsetTop) < 0 ? 0 : (this.getScrollTop() + this.getClientHeight() - scroolCon.offsetTop)))
      }
      if (scroolConScrol - this.getScrollTop() > 0) {
        if ((this.getScrollHeight() - this.getScrollTop() + this.getClientHeight()) >= 10) {
          debugger
          // this.downScroolShow = true
        } else {
          // this.downScroolShow = false
        }
      }
    },

 

你可能感兴趣的:(前端)