Hi,
I am using GSAP for a NetxJS project and I am having an issue with the positioning of my markers when I change the route. I tried to use the refresh() function when the routeChangeStart has occurred but that doesn't seem to be doing anything.
If I manually refresh the page then my markers are set correctly.
import Image from "../atoms/Image";
import Paragraph from "../atoms/Paragraph";
import { useRef, useEffect } from "react";
import { gsap } from "gsap";
import { ScrollTrigger } from "gsap/dist/ScrollTrigger";
import { useRouter } from "next/router";
gsap.registerPlugin(ScrollTrigger);
const ScrollRevealImageBlock = ({
text,
src,
fallbackSrc,
src2,
fallbackSrc2,
}) => {
const divRef = useRef(null);
const imageFrontRef = useRef(null);
const imageBehindRef = useRef(null);
const imagesContainer = useRef(null);
useEffect(() => {
const tl = gsap.timeline({
scrollTrigger: {
trigger: imageFrontRef.current,
start: "center center",
pin: imagesContainer.current,
pinSpacing: false,
pinnedContainer: imagesContainer.current,
endTrigger: divRef.current,
end: "bottom bottom",
scrub: 1,
markers: true,
},
});
tl.to(imageFrontRef.current, {
opacity: 1,
})
.to(imageBehindRef.current, {
opacity: 0,
})
.to(imageBehindRef.current, {
opacity: 1,
});
});
// when the route(page) changes re-run the scrollTrigger so the markers align properly
const router = useRouter();
useEffect(() => {
const pageChanged = () => {
console.log("refreshed");
ScrollTrigger.refresh();
};
router.events.on("routeChangeStart", pageChanged);
}, [router.events]);
return (
<div ref={divRef} className="scroll-reveal-image-block">
<div className="left-container site-width w-100 flex justify-end">
{text && <Paragraph content={text} />}
</div>
<div ref={imagesContainer} className="right-container">
<div ref={imageFrontRef} className="image-front image">
<Image src={src} fallbackSrc={fallbackSrc} />
</div>
<div ref={imageBehindRef} className="image-behind image">
<Image src={src2} fallbackSrc={fallbackSrc2} />
</div>
</div>
</div>
);
};
export default ScrollRevealImageBlock;
Many thanks,
Klaus