ViYaYaYa / gist

gist的入口太隐蔽了,以后在这里写!
0 stars 0 forks source link

用vue的method实现排名滚动效果 #12

Open ViYaYaYa opened 5 years ago

ViYaYaYa commented 5 years ago

因为transform的translateY100%会露出一大堆空白的东西,所以改由js实现,因此列表的transform: translate(-${scrollLineHeight * scrollIndex}px)

doScroll() {
  const rBox = this.$refs.box
  const rList = this.$refs.list
  if (!rBox || !rList) return
  if (rList.offsetHeight > rBox.clientHeight) {
    let lineH = this.scrollLineHeight = rList.firstChild.offsetHeight
    let boxH = rBox.clientHeight
    let listH = rList.offsetHeight
    const cb = () => {
      const i = ++this.scrollIndex
      if (i * lineH + boxH >= listH) {
        // 重新计算
        this.scrollIndex = 0
        lineH = this.scrollLineHeight = rList.firstChild.offsetHeight
        boxH = rBox.clientHeight
        listH = rList.offsetHeight
      }
      scrollTimer = setTimeout(cb, 1500)
    }
    cb()
  } else {
    clearTimeout(scrollTimer)
    scrollTimer = null
    this.scrollIndex = 0
  }
}