我是一名初级开发人员,正在努力解决页面顶部有大约 10 张卡片的轮播的问题。所有卡片无法同时显示在屏幕上,因此角落有 2 个箭头可滚动它们(向左箭头和向右箭头)。当您单击向右箭头时,卡片会滚动到末尾,当您单击向左箭头时,卡片会从右向左向后移动。
我怎样才能使当您单击鼠标并按住图像并拖动它们时它们向左或向右移动?我不仅需要通过单击箭头来移动它们,还需要通过鼠标拖动来移动它们...我是否需要使用一些库(我找到了react-hammerjs,但没有找到任何好的文档/示例如何使用它)?预先感谢您的帮助
这里有一些代码可供参考:
export const CardsBlock = () => {
const scrollContainer = useRef(null)
const [scrolled, setScrolled] = useState(false)
const dispatch = useDispatch()
useEffect(() => {
const resizeListener = (e) => {
if (e.target.outerWidth <= sizes.mobile) {
setScrolled(null)
} else {
setScrolled(false)
}
}
window.addEventListener('resize', resizeListener)
return () => {
window.removeEventListener('resize', resizeListener)
}
}, [])
useEffect(() => {
if (scrolled) {
scrollTo(scrollContainer.current, scrollContainer.current.offsetWidth)
} else {
scrollTo(scrollContainer.current, 0)
}
}, [scrolled])
return (
<Well>
<Container>
<Wrapper …Run Code Online (Sandbox Code Playgroud)