Logo

dev-resources.site

for different kinds of informations.

Creating Interactive Hover Effects with GSAP and SplitType

Published at
8/8/2024
Categories
html
css
javascript
gsap
Author
taiwo17
Categories
4 categories in total
html
open
css
open
javascript
open
gsap
open
Author
7 person written this
taiwo17
open
Creating Interactive Hover Effects with GSAP and SplitType

Introduction

In modern web development, creating engaging and interactive animations can significantly enhance user experience. The GreenSock Animation Platform (GSAP) is a powerful tool for creating high-performance animations. Combined with SplitType, a text splitting library, developers can create dynamic text animations with ease. In this article, weโ€™ll explore how to create a hover effect using GSAP and SplitType.

Check out the Video on YouTube

Creating Interactive Hover Effects with GSAP and SplitType

Project Structure

We'll be working with three files:

  1. index.html - The structure of our webpage.
  2. style.css - The styling of our webpage.
  3. main.js - The script where GSAP and SplitType are used for animations.

HTML: Structuring the Webpage

The HTML file provides the basic structure for our project. We have a title, a header, and a container with several colored boxes.

<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>GSAP Practice</title>
    <link rel="stylesheet" href="style.css" />
  </head>
  <body>
    <h1 id="hover-text">onHover effect with GSAP</h1>

    <div id="box-container">
      <div class="box green"></div>
      <div class="box purple"></div>
      <div class="box red"></div>
      <div class="box navy"></div>
      <div class="box orange"></div>
    </div>

    <script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/gsap.min.js"></script>
    <script src="https://unpkg.com/split-type"></script>
    <script src="main.js"></script>
  </body>
</html>
Enter fullscreen mode Exit fullscreen mode
  • Heading (h1): The heading displays a title with the ID hover-text, which will be animated using SplitType and GSAP.
  • Box Container (div#box-container): This container holds five different boxes with varying background colors. These boxes will have hover animations applied to them using GSAP.

CSS: Styling the Webpage

The CSS file defines the styles for the webpage, including the layout of the boxes and the initial appearance of the text.

* {
  margin: 0;
  padding: 0;
  box-sizing: border-box;
}

body {
  background-color: #eee;
}

h1 {
  text-align: center;
  margin-bottom: 40px;
  color: black;
  font-size: 3rem;
}

#box-container {
  display: flex;
  flex-direction: row;
  justify-content: center;
  align-items: center;
  gap: 20px;
  cursor: pointer;
}

.box {
  height: 100px;
  width: 100px;
}

.box.green {
  background-color: green;
}

.box.purple {
  background-color: purple;
}

.box.red {
  background-color: red;
}

.box.navy {
  background-color: navy;
}

.box.orange {
  background-color: orange;
}

.char {
  transform: translateY(-115px);
  transition: transform 0.5s;
}
Enter fullscreen mode Exit fullscreen mode
  • Global Styles: Resetting margin, padding, and box-sizing to ensure consistent styling across browsers.
  • Body: Setting a light grey background for the entire page.
  • Heading (h1): Center-aligning the text with a large font size and adding space below it.
  • Box Container (#box-container): Aligning the boxes horizontally in the center of the page with some spacing between them.
  • Boxes (.box): Defining the height and width of each box and applying different background colors.
  • Characters (.char): Initially setting each characterโ€™s position above its final position. The transition will make the animation smooth when the characters fall into place.

Check out the Video on YouTube

Creating Interactive Hover Effects with GSAP and SplitType

JavaScript: Adding Animations with GSAP and SplitType

The JavaScript file is where the magic happens. We use GSAP to create animations and SplitType to split the text into individual characters for more granular animation control.

/* Working on the text */
const myText = new SplitType('#hover-text')

gsap.to('.char', {
  y: 0,
  stagger: 0.05,
  delay: 0.2,
  duration: 1,
})

/* Working on the hover effect using GSAP */
const boxes = document.querySelectorAll('.box')

boxes.forEach((element) => createHover(element))

function createHover(element) {
  var tl = gsap.timeline({ paused: true, reversed: true })

  tl.to(element, { opacity: 1, duration: 0.3, height: 150 })

  element.addEventListener('mouseenter', () => {
    tl.reversed() ? tl.play() : tl.reverse()
  })

  element.addEventListener('mouseleave', () => {
    tl.reversed() ? tl.play() : tl.reverse()
  })
}
Enter fullscreen mode Exit fullscreen mode

Text Animation with SplitType and GSAP

  • SplitType: The SplitType library is used to split the text in the #hover-text element into individual characters. This allows for independent control of each character.
  • GSAP Animation: GSAP's gsap.to() method is then used to animate these characters. Each character animates by moving vertically into its place (y: 0) with a slight delay between each (stagger: 0.05). The animation begins with a small delay (delay: 0.2) and lasts for 1 second (duration: 1).

Hover Effect Animation on Boxes

  • Selecting Boxes: We select all elements with the class .box and iterate over them to apply the hover effect.
  • Creating Hover Timeline: For each box, a GSAP timeline is created. This timeline is paused initially (paused: true) and starts in the reversed state (reversed: true).
  • Hover In (mouseenter): When the mouse enters a box, the timeline plays forward, increasing the boxโ€™s height to 150px and slightly increasing its opacity.
  • Hover Out (mouseleave): When the mouse leaves the box, the timeline reverses, returning the box to its original state.

Conclusion

This simple project demonstrates how to create engaging animations using GSAP and SplitType. The hover effect applied to the boxes and the text animation adds a layer of interactivity that can make a webpage more dynamic and appealing. By understanding and leveraging the power of GSAP, you can create smooth, high-performance animations that enhance the user experience on your websites.

Whether youโ€™re a beginner or an experienced developer, experimenting with GSAP and SplitType will open up a world of creative possibilities in web animation.

Check out the Video on YouTube

Creating Interactive Hover Effects with GSAP and SplitType

gsap Article's
30 articles in total
Favicon
How to improve the Frontend part of the project using one button as an example :))))
Favicon
Reflections on My Coding Journey: Lessons from Recent Weeks
Favicon
Circle Cursor Js #GSAP
Favicon
"Day 7 with GSAP: Interactive Scrolling Animation with Rotating Arrows"
Favicon
GSAP ScrollTrigger animation for Upwork Client Demo
Favicon
Day 6: Text Animation with GSAP โ€“ A Split Letter Magic! ๐ŸŽจโœจ
Favicon
๐Ÿš€ Day 5: Creating a Hamburger Menu with GSAP
Favicon
Day 4: Enhancing UX with a Custom Cursor Using GSAP ๐Ÿš€๐ŸŽจ
Favicon
"Unlocking the Power of GSAP for Stunning Web Animations" ๐ŸŽจโœจ
Favicon
Preloader Animation #GSAP
Favicon
GSAP: GreenSock Animation Platform ๐Ÿš€
Favicon
Animated Carousel
Favicon
๐Ÿš€ ๐—ง๐—ต๐—ฒ๐—ฅ๐—ฒ๐—ฎ๐—น๐—›๐—ผ๐˜๐—ฒ๐—น๐˜€ ๐ŸŒŸ
Favicon
Clip-Path Circle Reveal Animation With Mouse Movement
Favicon
Getting started with Gsap!
Favicon
Todayโ€™s new knowledge #7(GSAP)
Favicon
Image Reveal Animation with HTML, CSS, and GSAP
Favicon
Easy React Animation with GSAP
Favicon
MoonAlert A CSS Animated Moon Notification Box
Favicon
Creating a Mesmerizing Moonlit Night Sky Animation with CSS
Favicon
๐Ÿš€ Motion Scape With GSAP
Favicon
Pinning Images with GSAP: A Smooth Scrolling Animation in Next.js
Favicon
Learning GSAP with a Day-to-Night Scroll Animation ๐ŸŒž๐ŸŒœ
Favicon
From Static to Stunning: Animate with GSAP
Favicon
Blurry Card with Parallax Effect ๐Ÿ’ณ
Favicon
Fun Animation using GSAP
Favicon
Future Bank Fintech Green: The Intersection of Finance and Sustainabilit
Favicon
Framer ground.
Favicon
Parallax image scroll speed with Nuxt 3 and GSAP ๐Ÿ›ฃ๏ธโœจ
Favicon
Creating Interactive Hover Effects with GSAP and SplitType

Featured ones: