ScrollTrigger pin video animation in GSAP, Reactjs

GSAP video animation : 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

Video stack animation in GSAP, Reactjs
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.

				
					useEffect(() => {
		let ctx = gsap.context(() => {
			let video = document.querySelector(".video");

			function toggleVideo(action) {
				let state = Flip.getState(video);
				video.classList[action]("fixed");
				Flip.from(state, {
					scale: true,
					ease: "power3.out",
					duration: 1
				});
			}
			ScrollTrigger.create({
				trigger: ".video-container",
				start: "90% top",
				// markers: true,
				onEnter: () => toggleVideo("add"),
				onLeaveBack: () => toggleVideo("remove")
			});
		});
		return () => ctx.revert();

	}, [])
				
			

Here, set scrollTrigger to .video-container class, while it’s trigger call function toggleVideo() and pass value “add” , so it’s fixed position of video. 

Full Source code
				
					import { Container, Typography } from "@mui/material";
import { Box } from "@mui/system";
import React, { useEffect } from "react";
import gsap from "gsap";
import Flip from "gsap/Flip";
import ScrollTrigger from "gsap/ScrollTrigger";

gsap.registerPlugin(Flip, ScrollTrigger);

const HomeVideo = () => {
	useEffect(() => {
		let ctx = gsap.context(() => {
			let video = document.querySelector(".video");

			function toggleVideo(action) {
				let state = Flip.getState(video);
				video.classList[action]("fixed");
				Flip.from(state, {
					scale: true,
					ease: "power3.out",
					duration: 1
				});
			}
			ScrollTrigger.create({
				trigger: ".video-container",
				start: "90% top",
				// markers: true,
				onEnter: () => toggleVideo("add"),
				onLeaveBack: () => toggleVideo("remove")
			});
		});
		return () => ctx.revert();

	},[])
	return (
		<Box sx={{ pt: 15, pb: 10 }}>
			<Container maxWidth="lg">
                <Typography variant="h2" component="h3" sx={{ textAlign: "center" }}>BRANDING</Typography>
                <Typography variant="body2" component="p" sx={{ textAlign: "center", mt: 3 }}>
					Lorem ipsum dolor, sit amet consectetur adipisicing elit. Tempore nulla aut provident earum quae obcaecati id numquam cupiditate soluta perferendis blanditiis officiis veritatis, beatae nesciunt iste aperiam atque repellat nihil!'
					'
				</Typography>

				<Box className="video-container" sx={{
					mt: 5,
					"&::before": {
						float: "left",
						paddingTop: "56.20608899297423%",
						content: `" "`,
					},
					"&::after": {
						display: "block",
						content: `" "`,
						clear: "both",
					},
					"& .video": {
						width: "100%",
						height: "auto",
						verticalAlign: "bottom",
						willChange: "transform",
						"&.fixed": {
							width: "300px",
							position: "fixed",
							bottom: "10px",
							left: "10px",
						}
					}
				}}>
					<video autoplay loop controls className="video" src="https://assets.codepen.io/16327/smol-vid.webm">
					</video>
				</Box>
			</Container>
		</Box >
	)
}

export default HomeVideo;
				
			
Scroll to Top