Skip to content

Instantly share code, notes, and snippets.

@lili668668
Created August 1, 2019 16:13
Show Gist options
  • Save lili668668/e641ec5ee38d1fc97275f03b775dfcb9 to your computer and use it in GitHub Desktop.
Save lili668668/e641ec5ee38d1fc97275f03b775dfcb9 to your computer and use it in GitHub Desktop.
計算表格位置的程式碼
offsetBody () {
const scrollTop = (this.$refs.viewport as Element).scrollTop
const remainTopNumber = Math.floor(this.remainLineNumber / 2)
const remainTopHeight = remainTopNumber * this.renderLineHeight
const remainBottomHeight = (this.remainLineNumber - remainTopNumber) * this.renderLineHeight
const thresholdHeight = this.thresholdNumber * this.renderLineHeight
const gap = scrollTop - this.bodyTranslateY
const overThreshold = gap < remainTopHeight - thresholdHeight || gap > remainTopHeight + thresholdHeight
const isReachBottom = this.totalHeight - (scrollTop + this.viewportHeight) < remainBottomHeight
const isReachTop = scrollTop < remainTopHeight
if (isReachTop) {
this.offset = 0
this.bodyTranslateY = 0
} else if (isReachBottom) {
this.offset = this.items.length - this.renderLineNumber
this.bodyTranslateY = this.totalHeight - this.renderBodyHeight
} else if (overThreshold) {
this.offset = Math.floor(scrollTop / this.renderLineHeight) - remainTopNumber
this.bodyTranslateY = scrollTop - remainTopHeight
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment