DEV Community

Cover image for How to Create Scroll Animation for Text in Your Website
Md Shakil Hossain
Md Shakil Hossain

Posted on

1 1 1 1 1

How to Create Scroll Animation for Text in Your Website

Title: How to Create Scroll Animation for Text in Your Website


Introduction

Scroll animations can add a dynamic and interactive element to your website, enhancing user engagement. In this post, we’ll create a scroll animation for text using JavaScript and CSS, suitable for both beginners and experienced developers. By the end, you’ll know how to implement a smooth text animation triggered by scrolling.


Features of Scroll Animation

  • Captures user attention with smooth transitions.
  • Fully responsive and optimized for modern browsers.
  • Easily customizable for different design needs.

Step-by-Step Process

1. Project Setup

Create a basic project structure with the following files:

project/
│
├── index.html
├── style.css
└── script.js
Enter fullscreen mode Exit fullscreen mode

2. HTML Setup

Create the basic structure in index.html for your animated text.

<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Scroll Animation Text</title>
  <link rel="stylesheet" href="style.css">
</head>
<body>
  <div class="container">
    <div class="scroll-text">
      <h1>Welcome to the World of Animation!</h1>
    </div>
    <div class="content">
      <p>Scroll down to see the magic!</p>
    </div>
  </div>
  <script src="script.js"></script>
</body>
</html>
Enter fullscreen mode Exit fullscreen mode

3. CSS Styling

Style the text and container in style.css.

body {
  margin: 0;
  padding: 0;
  font-family: Arial, sans-serif;
  background-color: #f4f4f9;
  overflow-x: hidden;
}

.container {
  height: 100vh;
  display: flex;
  align-items: center;
  justify-content: center;
  flex-direction: column;
}

.scroll-text h1 {
  font-size: 3rem;
  opacity: 0;
  transform: translateY(50px);
  transition: opacity 0.6s ease-out, transform 0.6s ease-out;
}

.content {
  margin-top: 200px;
  text-align: center;
}

.content p {
  font-size: 1.2rem;
  color: #555;
}
Enter fullscreen mode Exit fullscreen mode

4. Add JavaScript for Scroll Animation

Write the scroll-triggered animation logic in script.js.

document.addEventListener("DOMContentLoaded", () => {
  const scrollText = document.querySelector(".scroll-text h1");

  window.addEventListener("scroll", () => {
    const scrollPosition = window.scrollY;
    const screenHeight = window.innerHeight;

    if (scrollPosition > screenHeight / 4) {
      scrollText.style.opacity = "1";
      scrollText.style.transform = "translateY(0)";
    } else {
      scrollText.style.opacity = "0";
      scrollText.style.transform = "translateY(50px)";
    }
  });
});
Enter fullscreen mode Exit fullscreen mode

Customization Tips

  • Animation Timing: Adjust the transition properties in CSS for slower or faster animations.
  • Trigger Point: Modify screenHeight / 4 in the JavaScript logic for different scroll positions.
  • Font Style: Update the font-size and color in CSS to match your branding.

Final Output

When you scroll, the heading will smoothly appear as you reach a certain scroll position, creating a visually appealing effect.

``
Enter fullscreen mode Exit fullscreen mode

Conclusion

Adding scroll-triggered animations to your website is a simple yet effective way to enhance user experience. This approach is fully customizable, responsive, and lightweight, ensuring smooth performance across devices. Try this on your next project and make your site stand out!

Happy coding! 😊

AWS GenAI LIVE image

Real challenges. Real solutions. Real talk.

From technical discussions to philosophical debates, AWS and AWS Partners examine the impact and evolution of gen AI.

Learn more

Top comments (1)

Collapse
 
crazyboyze profile image
crazyboyze • Edited

To create a scroll animation for text on your website, use CSS animations with the @keyframes rule. For example, animate the text to slide in from the left as the user scrolls. This can enhance user engagement on your site, especially when showcasing products like used auto spare parts Sharjah offers a wide range of lighting solutions, specializing in high-quality products for residential and commercial spaces, with a focus on innovation and customer satisfaction.

Tiger Data image

🐯 🚀 Timescale is now TigerData: Building the Modern PostgreSQL for the Analytical and Agentic Era

We’ve quietly evolved from a time-series database into the modern PostgreSQL for today’s and tomorrow’s computing, built for performance, scale, and the agentic future.

So we’re changing our name: from Timescale to TigerData. Not to change who we are, but to reflect who we’ve become. TigerData is bold, fast, and built to power the next era of software.

Read more

👋 Kindness is contagious

Dive into this thoughtful piece, beloved in the supportive DEV Community. Coders of every background are invited to share and elevate our collective know-how.

A sincere "thank you" can brighten someone's day—leave your appreciation below!

On DEV, sharing knowledge smooths our journey and tightens our community bonds. Enjoyed this? A quick thank you to the author is hugely appreciated.

Okay