The slight stutter you're experiencing when scrolling back up to re-pin the box is likely due to the way you're updating the position of the box with top: pinBox
&& scrollY
. This expression is causing the box to jump to the current scroll position whenever pinBox
changes from false
to true
. You can use useTransform
hook in this kind of situation.
const { scrollY } = useScroll();
const [pinBox, setPinBox] = useState(true);
const boxY = useTransform(
scrollY,
[0, window.innerHeight],
[0, window.innerHeight]
);
And apply following changes inside motion.div
component's style
prop
<motion.div
layout
style={{
position: "absolute",
top: pinBox ? boxY : "auto",
bottom: pinBox ? "auto" : 0,
left: "50%",
width: "100px",
height: "100px",
backgroundColor: "red",
x: "-50%"
}}
/>
This should fix the stutter.