profile picture of Olivier Larose

Olivier Larose

March 27, 2023

/

Beginner

/

Short

Project Gallery Colored Card

How to Make a Text Hover Animation using NextJS and GSAP

A hover animation website tutorial using NextJs and GSAP on a colourful project gallery. Inspired by https://anacuna.com/

Live DemoSource codeVideo Tutorial
background video

Initializing the project

Let's start the project by creating a Next.js application. We can do that by running npx create-next-app@13.2.4 myApp inside of a terminal.

Adding the HTML and CSS

We can delete everything in the index.js and add our own HTML and CSS.

For the CSS, we're going to use CSS Module, so make sure to create a Home.module.css file inside of the styles folder. Thankfully, Next.js has built-in support for CSS Modules

Index.js

Home.mod...

1

import styles from '../styles/Home.module.css'

2

3

const projects = [

4

{

5

title: "New York",

6

color: "#F06318"

7

},

8

{

9

title: "San Francisco",

10

color: "#DCF018"

11

},

12

{

13

title: "San Antonio",

14

color: "#18F0E8"

15

},

16

{

17

title: "Nashville",

18

color: "#8C0CF0"

19

},

20

{

21

title: "Houston",

22

color: "#F0183C"

23

},

24

{

25

title: "New Orleans",

26

color: "#F0BA18"

27

},

28

{

29

title: "Oklahoma City",

30

color: "#0C34F0"

31

},

32

{

33

title: "Detroit",

34

color: "#0CBCF0"

35

},

36

{

37

title: "Indianapolis",

38

color: "#91F018"

39

}

40

]

41

42

43

export default function Home() {

44

return (

45

<div className={styles.container}>

46

<div className={styles.projectContainer}>

47

{

48

projects.map( (project, index) => {

49

return <div key={index}>

50

<p>{project.title}</p>

51

</div>

52

})

53

}

54

</div>

55

</div>

56

)

57

}

58

Here's a couple of important things to note about the CSS:

  • Line 16: We use negative margin to overlap the divs
  • Line 17: We give the each div a background-color: white to hide the div beneath it

We should have something like this

Screenshot of the HTML and CSS results

Animating In and Out

To animate the divs, we're going to use GSAP for it. To use the library, make sure to install it by using npm install gsap.

With the help of GSAP, we can adjust the divs properties to complete the animation:

Index.js

Home.mod...

1

...

2

3

export default function Home() {

4

5

const manageMouseEnter = (e, index) => {

6

gsap.to(e.target, {top: "-2vw", backgroundColor: projects[index].color, duration: 0.3})

7

}

8

9

const manageMouseLeave = (e, index) => {

10

gsap.to(e.target, {top: "0", backgroundColor: "white", duration: 0.3, delay: 0.1})

11

}

12

13

return (

14

<div className={styles.container}>

15

<div className={styles.projectContainer}>

16

{

17

projects.map( (project, index) => {

18

return <div onMouseEnter={(e) => {manageMouseEnter(e, index)}} onMouseLeave={(e) => {manageMouseLeave(e, index)}} key={index}>

19

<p>{project.title}</p>

20

</div>

21

})

22

}

23

</div>

24

</div>

25

)

26

}

27

Pretty simple right? Here's the keys to the animation:

  • Line 6: We animate in by adjusting the top property of the hovered div and give it its corresponding color.
  • Line 10: We animate out by resetting the top property to 0, and the background-color to white.
  • Line 5 (css): We add a pointer-events: none to the p to avoid any bugs

Wrapping up

That was it for this animation. Hope you learned a lot, it was quite a simple one, but understanding that negative margin is the key to this animation can be quite tricky. See you next time

Hope you learned a lot :)

- Oli

Related Animations

image

Jul 24, 2023

Awwwards Landing Page

An Awwwards portfolio landing page rebuild. Originally made by Dennis Snellenberg, he won an awwwards with his amazing portoflio. Remade the landing page using Next.js, Framer Motion and GSAP. See the original: https://dennissnellenberg.com/

image

June 11, 2023

Project Gallery Mouse Hover

An awwwards winning website tutorial with a project gallery featuring a hover animation using Nextjs, GSAP and Framer Motion. Inspired by: https://dennissnellenberg.com/

image

May 28, 2023

Infinite Text Move On Scroll

An infinite text moving animation with scroll interaction using React and Next.js. Picture by Eric Asamoah.