Приветствую.
Подскажите, как задать границы перетаскиваемого объекта внутри div, от 0% до 100%
Сейчас я могу его двигать бесконечное количество процентов вверх и вниз, а нужно вверх 0 вниз 100%
import { useEffect, useState } from "react";
const [isDragging, setIsDragging] = useState(false);
const [yTranslate, setYTranslate] = useState(0);
const [initialMousePosition, setInitialMousePosition] = useState(undefined);
const onMouseDown = ({ clientY }) => {
setInitialMousePosition({ y: clientY });
setIsDragging(true);
};
useEffect(() => {
const onMouseMove = (e) => {
setYTranslate(yTranslate + e.clientY - initialMousePosition.y);
};
if (isDragging) {
window.addEventListener("mousemove", onMouseMove);
}
return () => window.removeEventListener("mousemove", onMouseMove);
}, [isDragging, initialMousePosition]);
useEffect(() => {
const onMouseUp = () => setIsDragging(false);
window.addEventListener("mouseup", onMouseUp);
return () => window.removeEventListener("mouseup", onMouseUp);
}, []);
<div className="text-[15px]/[22px] bg-white">
<div className="relative aspect-[640_/_200] bg-[#e5e5e5] overflow-hidden" style={{'coverY': yTranslate + '%'}}>
<div className="w-full h-full overflow-hidden grid grid-cols-[100%] grid-rows-[100%]" style={{aspectRatio: '1 / 1', maxWidth: 'none'}}>
<picture className="block w-full h-full col-[1] row-[1]">
<img src="/1.jpg" alt="" className="object-[0_var(coverY,50%)] object-cover w-full h-full" />
</picture>
</div>
<div className="group flex absolute w-full h-full select-none left-0 top-0 cursor-move">
<div className="absolute w-full h-full z-[1] left-0 top-0" onMouseDown={onMouseDown}></div>
</div>
</div>
</div>