滚动条 scrollTop 平滑滑动


vue 写法 平滑滑动
 
  gotoRirser (num, index) {
      let that = this
      let elt = this.elt
      let total = 0
      let distance = 0
      let step = 14
      let enterBox = this.enterBox
      function smoothDown () {
        let td = parseInt((total - distance) / 10)
        if (td > step) {
          distance = distance + td
          enterBox.scrollTop = distance
          setTimeout(smoothDown, 10)
        } else {
          enterBox.scrollTop = total
          that.leftTIndex = setTimeout(() => {
            that.scrollLock = true
            that.setLiTyDatasAll()
          }, 1000)
        }
      }

      // 向上滑动
      function smoothUp () {
        let td = (distance - total) / 10
        if (td > step) {
          distance -= td
          enterBox.scrollTop = distance
          setTimeout(smoothUp, 10)
        } else {
          enterBox.scrollTop = total
          that.rightTIndex = setTimeout(() => {
            that.scrollLock = true
            that.setLiTyDatasAll()
          }, 1000)
        }
      }
      setTimeout(() => {
        for (let i = index - 1; i >= 0; i--) {
          let n = this.allBtns[i].num
          let eNode = enterBox.querySelector('#' + elt + n)
          total += eNode.clientHeight
        }
        distance = this.enterBox.scrollTop
        this.scrollLock = false
        if (this.rightTIndex) {
          clearTimeout(this.rightTIndex)
        }
        if (this.leftTIndex) {
          clearTimeout(this.leftTIndex)
        }
        if (total > distance) {
          smoothDown()
        } else {
          smoothUp()
        }
      }, 20)
    },
View Code

  


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM