Initializing the project
Let's start the project by creating a Next.js application. We can do that by running npx create-next-app@latest client
inside of a terminal.
Adding the HTML and CSS
We can delete everything in the page.js
, global.css
and page.module.css
and add our own HTML and CSS, to start with a nice blank application.
2
import styles from './page.module.css'
3
import { useRef } from 'react';
5
const phrase = "It is a long established fact that a reader will be distracted by the readable content of a page when looking at its layout. The point of using Lorem Ipsum is that it has a more-or-less normal distribution of letters.";
7
export default function Home() {
10
const container = useRef(null);
12
const splitWords = (phrase) => {
14
phrase.split(" ").forEach( (word, i) => {
15
const letters = splitLetters(word);
16
body.push(<p key={word + "_" + i}>{letters}</p>)
21
const splitLetters = (word) => {
23
word.split("").forEach( (letter, i) => {
24
letters.push(<span key={letter + "_" + i} ref={el => {refs.current.push(el)}}>{letter}</span>)
30
<main ref={container} className={styles.main}>
31
<div ref={body} className={styles.body}>
We should have something like this:
Incrementing the opacity on scroll
To gradually increment the opacity of each characters, we will use the ScrollTrigger plugin from GSAP.
We can easily animate each characters individually by providing the refs
array and give a stagger
value to the gsap functions:
2
import { useRef, useEffect } from 'react';
3
import { ScrollTrigger } from 'gsap/ScrollTrigger';
4
import gsap from 'gsap';
7
const container = useRef(null);
10
gsap.registerPlugin(ScrollTrigger);
14
const createAnimation = () => {
15
gsap.to(refs.current, {
17
trigger: container.current,
20
end: `+=${window.innerHeight / 1.5}`,
Note that if we'd like to adjust the gradient, the stagger
value is the one to adjust.
We should have the final animation:
Wrapping up
I've seen this animation in about 50% of every single awwwards winning website of May 2023, so I thought it would be interesting to remake it! Hope you learned something :)
-Oli