问题
基本上我有一个有一些视频的移动网站。每个视频占用 100% 的宽度和 100% 的高度。我正在尝试制作滚动捕捉类型的提要,用户一次只能滚动一个视频,如果他们在滚动时到达页面上的某个点,它将捕捉到下一个视频。这有点像 tiktok 的视频提要或 instagram 卷轴作为一个想法。
我正在使用 scroll-snap 包,它让我达到了我想要实现的目标的一半。缓慢滚动时它会卡住,但是如果我在移动设备上,我可以非常快速地滚动并让滚动势头跳过视频。我正在努力做到这一点,以便用户一次只能滚动 1 个视频,无论他们滚动多么努力。
这是正在发生的事情:https ://streamable.com/f98slq 。如您所见,我一次可以滚动超过 1 个视频。
我试过的
我试图在滚动提要时获得滚动速度。如果它高于某个值,我会应用一种停止页面滚动 10 毫秒的样式。这并没有真正起作用,因为即使滚动速度不高,滚动动量也能够滚动页面。我不是很喜欢这个解决方案。
我不确定解决这个问题的最佳方法。对于任何反馈,我们都表示感谢。
这是我的代码:
// Scroll snap
useEffect(() => {
const { unbind } = createScrollSnap(MobileSnapContainer.current!, {
snapDestinationX: '0%',
snapDestinationY: '100%',
timeout: 0,
duration: 100,
threshold: 0.1,
snapStop: true,
}, () => {})
return () => unbind()
}, [])
return (
<>
<InfiniteScroll
dataLength={mobileVideos.length}
next={loadMore}
hasMore={hasMore}
loader={null}
scrollableTarget="container"
>
<div
className="overflow-auto select-none hide-scrollbar"
id="container"
ref={MobileSnapContainer}
>
{mobileVideos.map((data: video, index: number) => (
<SingleMobileVideo
index={index}
/>
))}
</div>
</InfiniteScroll>
</>