I need this yellow sticky box to be position fixed until the scroll reaches the footer. when the footer hits the screen, the sticky box should scroll with the dynamic content box and vise versa. I tried position: sticky
but no luck. javascript solutions will be also OK.
I also tried the below code also, but it will not work smoothly, since position: relative
makes the sticky-content-box jump directly to the top, not scroll with the left side content.
const height = 900; // this would be calculated dynamic content box height
const backGroundAnimation = useCallback(() => {
if (window.scrollY > height) {
fixedBox!.current!.style.position = 'relative';
fixedBox!.current!.style.marginTop = `${height}px`;
} else {
fixedBox!.current!.style.position = 'fixed';
}
}, [height]);
useEffect(() => {
document.addEventListener('scroll', backGroundAnimation);
return () => document.removeEventListener('scroll', backGroundAnimation);
}, [backGroundAnimation]);
here's a code sandbox setup I have
please note that with the codesandbox
implementation I did not include the functions I added here in the question.