Pinned image animation in GSAP, Reactjs

Create pinned image animation in GSAP, Reactjs : GSAP is an industry standard JavaScript animation library from GreenSock that lets you craft high-performance animations that work in every major browser.

Please Like and subscribe for more GSAP animation – Youtube video

Lets, Create a ReactJS project and install gsap :
  • You can create a new ReactJS project using Create React App or any other preferred method. If you haven’t installed Create React App globally, you can do so with the following command: npm install -g create-react-app
  • Then create a new React project:  npx create-react-app my-gsap-project
  • go to project folder :  cd my-gsap-project
  • Next, you need to install GSAP package. You can do this via npm or yarn: npm install gsap or yarn add gsap

 

Here are some basic steps to follow when creating GSAP animation in Reactjs:
  1. Register the scrollTrigger plugin to trigger elements on scroll.

  2. Create React Hooks and write GSAP code inside them.

  3. Utilize gsap.context() to collect all GSAP animations and ScrollTriggers created within the supplied function, allowing for easy reverting or killing of ALL of them at once.

  4. Develop timelines and scrollTriggers based on your specific requirements.

GSAP logic
				
					useEffect(() => {
		let ctx = gsap.context(() => {
			gsap.set(".photo:not(:first-child)", { opacity: 0, scale: 0.5 })

			const animation = gsap.to(".photo:not(:first-child)", {
				opacity: 1, scale: 1, duration: 1, stagger: 1
			})

			ScrollTrigger.create({
				trigger: ".gallery",
				start: "top top",
				end: "bottom bottom",
				pin: ".rightblock",
				animation: animation,
				scrub: true,
				markers: true,
			})
		})
		return () => ctx.revert();
	}, [])
				
			
JSX / HTML Code
				
					<React.Fragment>
			<Box className="gallery" sx={{ display: "flex" }}>
				<Box className="left" sx={{
					width: "50%",
					marginLeft: "auto",
					"& .details": {
						height: "100vh",
						display: "flex",
						flexDirection: "column",
						justifyContent: "center",
						width: "40vw",
						marginLeft: "auto",
						color: "#000",
						fontSize: "3rem",
						fontWeight: 900,
					}
				}}>
					<Box className="details">
						BRAND PRODUCT
					</Box>
					<Box className="details">
						PRODUCT DETAILS
					</Box>
					<Box className="details">
						DESIGN AGENCY
					</Box>
				</Box>
				<Box className="rightblock" sx={{
					width: "50%",
					height: "100vh",
					display: "flex",
					flexDirection: "column",
					justifyContent: "center",
				}}>
					<Box sx={{
						width: "40vw",
						height: "40vw",
						position: "relative",
						"& .photo": {
							position: "absolute",
							width: "100%",
							height: "100%",
							"& img": {
								height: "100%",
								width: "100%",
							}
						}
					}}>
						<Box className="photo">
							<img
								src="http://static.showit.co/800/M0IzUCMvQCqlJn1YtNlikw/59514/pexels-yan-5793641-1.jpg"
								alt="img-1" />
						</Box>
						<Box className="photo">
							<img
								src="http://static.showit.co/800/137NFxuzSxeyjlons2WEzA/59514/pexels-yan-5793643.jpg"
								alt="img-2" />
						</Box>
						<Box className="photo">
							<img
								src="http://static.showit.co/800/3kOLYaOCS1SMieN6Y88Fqg/59514/mukuko-studio-mu88mlefcou-unsplash.jpg"
								alt="img-3" />
						</Box>
					</Box>
				</Box>
			</Box>
		</React.Fragment >
				
			
Full Source code
				
					import React from "react";
// import { TweenMax, Power3, Power4 } from "gsap";
import { useEffect } from "react";
import gsap from "gsap";
import ScrollTrigger from "gsap/ScrollTrigger";
import Box from "@mui/material/Box";

gsap.registerPlugin(ScrollTrigger);

function ServiceType() {
	useEffect(() => {
		let ctx = gsap.context(() => {
			gsap.set(".photo:not(:first-child)", { opacity: 0, scale: 0.5 })

			const animation = gsap.to(".photo:not(:first-child)", {
				opacity: 1, scale: 1, duration: 1, stagger: 1
			})

			ScrollTrigger.create({
				trigger: ".gallery",
				start: "top top",
				end: "bottom bottom",
				pin: ".rightblock",
				animation: animation,
				scrub: true,
				markers: true,
			})
		})
		return () => ctx.revert();
	}, [])
	return (
		<React.Fragment>
			<Box className="gallery" sx={{ display: "flex" }}>
				<Box className="left" sx={{
					width: "50%",
					marginLeft: "auto",
					"& .details": {
						height: "100vh",
						display: "flex",
						flexDirection: "column",
						justifyContent: "center",
						width: "40vw",
						marginLeft: "auto",
						color: "#000",
						fontSize: "3rem",
						fontWeight: 900,
					}
				}}>
					<Box className="details">
						BRAND PRODUCT
					</Box>
					<Box className="details">
						PRODUCT DETAILS
					</Box>
					<Box className="details">
						DESIGN AGENCY
					</Box>
				</Box>
				<Box className="rightblock" sx={{
					width: "50%",
					height: "100vh",
					display: "flex",
					flexDirection: "column",
					justifyContent: "center",
				}}>
					<Box sx={{
						width: "40vw",
						height: "40vw",
						position: "relative",
						"& .photo": {
							position: "absolute",
							width: "100%",
							height: "100%",
							"& img": {
								height: "100%",
								width: "100%",
							}
						}
					}}>
						<Box className="photo">
							<img
								src="http://static.showit.co/800/M0IzUCMvQCqlJn1YtNlikw/59514/pexels-yan-5793641-1.jpg"
								alt="img-1" />
						</Box>
						<Box className="photo">
							<img
								src="http://static.showit.co/800/137NFxuzSxeyjlons2WEzA/59514/pexels-yan-5793643.jpg"
								alt="img-2" />
						</Box>
						<Box className="photo">
							<img
								src="http://static.showit.co/800/3kOLYaOCS1SMieN6Y88Fqg/59514/mukuko-studio-mu88mlefcou-unsplash.jpg"
								alt="img-3" />
						</Box>
					</Box>
				</Box>
			</Box>
		</React.Fragment >
	);
}

export default ServiceType;
				
			
Scroll to Top