优化我的列表、下载列表等列表的滚动流畅度

pull/733/head
lyswhut 2021-10-30 12:16:58 +08:00
parent 58f016ca00
commit f0f92952d6
2 changed files with 16 additions and 13 deletions

View File

@ -1,3 +1,7 @@
### 优化
- 优化我的列表、下载列表等列表的滚动流畅度
### 修复 ### 修复
- 修复某些情况下获取URL失败时会意外切歌的问题 - 修复某些情况下获取URL失败时会意外切歌的问题

View File

@ -82,7 +82,7 @@ export default {
}, },
outsideNum: { outsideNum: {
type: Number, type: Number,
default: 10, default: 1,
}, },
itemHeight: { itemHeight: {
type: Number, type: Number,
@ -101,7 +101,6 @@ export default {
data() { data() {
return { return {
views: [], views: [],
isWaitingUpdate: false,
startIndex: -1, startIndex: -1,
endIndex: -1, endIndex: -1,
scrollTop: 0, scrollTop: 0,
@ -138,16 +137,15 @@ export default {
this.updateView() this.updateView()
}, },
beforeDestroy() { beforeDestroy() {
this.$refs.dom_scrollContainer.removeEventListener('scroll', this.onScroll)
if (this.cancelScroll) this.cancelScroll() if (this.cancelScroll) this.cancelScroll()
}, },
methods: { methods: {
onScroll(event) { onScroll(event) {
if (this.isWaitingUpdate) return const currentScrollTop = this.$refs.dom_scrollContainer.scrollTop
this.isWaitingUpdate = true if (Math.abs(currentScrollTop - this.scrollTop) > this.itemHeight * this.outsideNum * 0.6) {
window.requestAnimationFrame(() => { this.updateView(currentScrollTop)
this.updateView() }
this.isWaitingUpdate = false
})
this.$emit('scroll', event) this.$emit('scroll', event)
}, },
@ -168,17 +166,18 @@ export default {
return list return list
}, },
updateView() { updateView(currentScrollTop = this.$refs.dom_scrollContainer.scrollTop) {
const currentScrollTop = this.$refs.dom_scrollContainer.scrollTop // const currentScrollTop = this.$refs.dom_scrollContainer.scrollTop
const currentStartIndex = Math.floor(currentScrollTop / this.itemHeight) const currentStartIndex = Math.floor(currentScrollTop / this.itemHeight)
const currentEndIndex = currentStartIndex + Math.ceil(this.$refs.dom_scrollContainer.clientHeight / this.itemHeight) const scrollContainerHeight = this.$refs.dom_scrollContainer.clientHeight
const currentEndIndex = currentStartIndex + Math.ceil(scrollContainerHeight / this.itemHeight)
const continuous = currentStartIndex <= this.endIndex && currentEndIndex >= this.startIndex const continuous = currentStartIndex <= this.endIndex && currentEndIndex >= this.startIndex
const currentStartRenderIndex = Math.max(Math.floor(currentScrollTop / this.itemHeight) - this.outsideNum, 0) const currentStartRenderIndex = Math.max(Math.floor(currentScrollTop / this.itemHeight) - this.outsideNum, 0)
const currentEndRenderIndex = currentStartIndex + Math.ceil(this.$refs.dom_scrollContainer.clientHeight / this.itemHeight) + this.outsideNum const currentEndRenderIndex = currentStartIndex + Math.ceil(scrollContainerHeight / this.itemHeight) + this.outsideNum
// console.log(continuous) // console.log(continuous)
// debugger // debugger
if (continuous) { if (continuous) {
if (Math.abs(currentScrollTop - this.scrollTop) < this.itemHeight * this.outsideNum * 0.6) return // if (Math.abs(currentScrollTop - this.scrollTop) < this.itemHeight * this.outsideNum * 0.6) return
// console.log('update') // console.log('update')
if (currentScrollTop > this.scrollTop) { // scroll down if (currentScrollTop > this.scrollTop) { // scroll down
// console.log('scroll down') // console.log('scroll down')