[APP-1083] bug fix: videos not accurately autoplaying on web (#8692)
* update: auto play video on web with intersection position * place back the threshold: 0.5 * update: optimize the intersection observer with a throttled scroll listener --------- Co-authored-by: Anastasiya <anastasiya@Mac.localdomain> Co-authored-by: Anastasiya <anastasiya@Anastasiyas-MacBook-Pro.local>
This commit is contained in:
committed by
GitHub
parent
14e39c4aec
commit
9aa35e9fbb
@@ -138,22 +138,53 @@ function ViewportObserver({
|
|||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
if (!ref.current) return
|
if (!ref.current) return
|
||||||
if (isFullscreen && !isFirefox) return
|
if (isFullscreen && !isFirefox) return
|
||||||
|
|
||||||
|
let scrollTimeout: NodeJS.Timeout | null = null
|
||||||
|
let lastObserverEntry: IntersectionObserverEntry | null = null
|
||||||
|
|
||||||
|
const updatePositionFromEntry = () => {
|
||||||
|
if (!lastObserverEntry) return
|
||||||
|
const rect = lastObserverEntry.boundingClientRect
|
||||||
|
const position = rect.y + rect.height / 2
|
||||||
|
sendPosition(position)
|
||||||
|
}
|
||||||
|
|
||||||
|
const handleScroll = () => {
|
||||||
|
if (scrollTimeout) {
|
||||||
|
clearTimeout(scrollTimeout)
|
||||||
|
}
|
||||||
|
scrollTimeout = setTimeout(updatePositionFromEntry, 4) // ~240fps
|
||||||
|
}
|
||||||
|
|
||||||
const observer = new IntersectionObserver(
|
const observer = new IntersectionObserver(
|
||||||
entries => {
|
entries => {
|
||||||
const entry = entries[0]
|
const entry = entries[0]
|
||||||
if (!entry) return
|
if (!entry) return
|
||||||
const position =
|
lastObserverEntry = entry
|
||||||
entry.boundingClientRect.y + entry.boundingClientRect.height / 2
|
|
||||||
sendPosition(position)
|
|
||||||
setNearScreen(entry.isIntersecting)
|
setNearScreen(entry.isIntersecting)
|
||||||
|
const rect = entry.boundingClientRect
|
||||||
|
const position = rect.y + rect.height / 2
|
||||||
|
sendPosition(position)
|
||||||
},
|
},
|
||||||
{threshold: Array.from({length: 101}, (_, i) => i / 100)},
|
{threshold: [0, 0.1, 0.25, 0.5, 0.75, 1.0]},
|
||||||
)
|
)
|
||||||
observer.observe(ref.current)
|
|
||||||
return () => observer.disconnect()
|
|
||||||
}, [sendPosition, isFullscreen])
|
|
||||||
|
|
||||||
// In case scrolling hasn't started yet, send up the position
|
observer.observe(ref.current)
|
||||||
|
|
||||||
|
if (nearScreen) {
|
||||||
|
window.addEventListener('scroll', handleScroll, {passive: true})
|
||||||
|
}
|
||||||
|
|
||||||
|
return () => {
|
||||||
|
observer.disconnect()
|
||||||
|
if (scrollTimeout) {
|
||||||
|
clearTimeout(scrollTimeout)
|
||||||
|
}
|
||||||
|
window.removeEventListener('scroll', handleScroll)
|
||||||
|
}
|
||||||
|
}, [sendPosition, isFullscreen, nearScreen])
|
||||||
|
|
||||||
|
// In case scrolling hasn't started yet, send the original position
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
if (ref.current && !isAnyViewActive) {
|
if (ref.current && !isAnyViewActive) {
|
||||||
const rect = ref.current.getBoundingClientRect()
|
const rect = ref.current.getBoundingClientRect()
|
||||||
|
|||||||
Reference in New Issue
Block a user