body {
max-width: 100vw;
}
body::-webkit-scrollbar {
display: none;
}
const [startX, setStartX] = useState<number>();
const [startY, setStartY] = useState<number>();
const onDragStart = (e:any) => {
setStartX(e.pageX + bodyRef.current!.scrollLeft);
setStartY(e.pageY + bodyRef.current!.scrollTop);
};
const onDragMove = (e:any) => {
bodyRef.current!.scrollLeft = startX! - e.pageX;
bodyRef.current!.scrollTop = startY! - e.pageY;
};
<div>
ref={bodyRef}
onMouseDown={onDragStart}
onMouseMove={onDragMove}
onMouseUp={onDragEnd}
onMouseLeave={onDragEnd}
</div>
즉, startX = e.pageX + bodyRef.current!.scrollLeft - e.pageX 로 현재 스크롤 위치를 설정하겠다. 라는 것이다.
그래서 정리하면 startX = bodyRef.current!.scrollLeft 라는 깔끔한 식이 남는다.
const onTouchDragStart = (e:any) => {
if(dragMode.isMode===false){
return
}
dispatch(setIsDrag(true));
setStartX(e.touches[0].pageX + bodyRef.current!.scrollLeft);
setStartY(e.touches[0].pageY + bodyRef.current!.scrollTop);
};
const onTouchDragMove = (e:any) => {
if(dragMode.isMode===false){
return
}
if (dragMode.isDrag) {
bodyRef.current!.scrollLeft = startX! - e.touches[0].pageX;
bodyRef.current!.scrollTop = startY! - e.touches[0].pageY;
}
};