防止前端弹幕卡住

This commit is contained in:
John Smith 2020-02-04 20:13:11 +08:00
parent 0cf49ee042
commit b7e06652b4

View File

@ -1,8 +1,10 @@
<template>
<yt-live-chat-renderer class="style-scope yt-live-chat-app" style="--scrollbar-width:11px;" hide-timestamps>
<yt-live-chat-renderer class="style-scope yt-live-chat-app" style="--scrollbar-width:11px;" hide-timestamps
@mousemove="refreshCantScrollStartTime"
>
<ticker class="style-scope yt-live-chat-renderer" :messages="paidMessages" :hidden="paidMessages.length === 0"></ticker>
<yt-live-chat-item-list-renderer class="style-scope yt-live-chat-renderer" allow-scroll>
<div id="item-scroller" ref="scroller" class="style-scope yt-live-chat-item-list-renderer animated" @scroll="onScroll">
<div ref="scroller" id="item-scroller" class="style-scope yt-live-chat-item-list-renderer animated" @scroll="onScroll">
<div ref="itemOffset" id="item-offset" class="style-scope yt-live-chat-item-list-renderer" style="height: 0px;">
<div ref="items" id="items" class="style-scope yt-live-chat-item-list-renderer" style="overflow: hidden"
:style="{transform: `translateY(${Math.floor(scrollPixelsRemaining)}px)`}"
@ -86,7 +88,8 @@ export default {
smoothScrollRafHandle: null, // requestAnimationFrame
lastSmoothScrollUpdate: null, //
atBottom: true //
atBottom: true, //
cantScrollStartTime: null //
}
},
computed: {
@ -336,7 +339,7 @@ export default {
if (this.messagesBuffer.length <= 0) {
return
}
if (!this.canScrollToBottom) {
if (!this.canScrollToBottomOrTimedOut()) {
if (this.messagesBuffer.length > this.maxNumber) {
// >
this.messagesBuffer.splice(0, this.messagesBuffer.length - this.maxNumber)
@ -362,7 +365,7 @@ export default {
showNewMessages() {
let hasScrollBar = this.$refs.items.clientHeight > this.$refs.scroller.clientHeight
this.$refs.itemOffset.style.height = `${this.$refs.items.clientHeight}px`
if (!this.canScrollToBottom || !hasScrollBar) {
if (!this.canScrollToBottomOrTimedOut() || !hasScrollBar) {
return
}
@ -436,18 +439,43 @@ export default {
this.maybeScrollToBottom()
},
maybeScrollToBottom() {
if (this.canScrollToBottom) {
if (this.canScrollToBottomOrTimedOut()) {
this.scrollToBottom()
}
},
scrollToBottom() {
this.$refs.scroller.scrollTop = Math.pow(2, 24)
this.atBottom = true
if (this.canScrollToBottom) {
this.cantScrollStartTime = null
}
},
onScroll() {
this.refreshCantScrollStartTime()
let scroller = this.$refs.scroller
this.atBottom = scroller.scrollHeight - scroller.scrollTop - scroller.clientHeight < SCROLLED_TO_BOTTOM_EPSILON
if (this.canScrollToBottom) {
this.cantScrollStartTime = null
}
this.flushMessagesBuffer()
},
canScrollToBottomOrTimedOut() {
if (this.canScrollToBottom) {
this.cantScrollStartTime = null
return true
}
// OBS
if (!this.cantScrollStartTime) {
this.cantScrollStartTime = new Date()
return false
}
return new Date() - this.cantScrollStartTime >= 10 * 1000
},
refreshCantScrollStartTime() {
//
if (this.cantScrollStartTime) {
this.cantScrollStartTime = new Date()
}
}
}
}