DEV Community

Raj Aryan
Raj Aryan

Posted on

πŸš€ Scroll-Triggered Animations in CSS: Make Your Website Pop! πŸŽ‰

Hey devs! πŸ‘‹ Are you looking to add some pizzazz to your website? Scroll-triggered animations are a fantastic way to engage users and make your site feel dynamic and interactive. The best part? You can achieve this with just CSS! No JavaScript required. Let’s dive in and learn how to implement scroll-triggered animations on your website. 🌟


What Are Scroll-Triggered Animations?

Scroll-triggered animations are effects that activate as users scroll down a webpage. Think of elements fading in, sliding up, or scaling as they come into view. These subtle animations can significantly enhance user experience and make your site feel more polished.


How to Implement Scroll-Triggered Animations in CSS

Step 1: Use @keyframes to Define Your Animation

First, define the animation you want to trigger on scroll. For example, let’s create a fade-in effect:

@keyframes fadeIn {
  from {
    opacity: 0;
    transform: translateY(20px);
  }
  to {
    opacity: 1;
    transform: translateY(0);
  }
}
Enter fullscreen mode Exit fullscreen mode

Step 2: Apply the Animation to Your Element

Next, apply the animation to the element you want to animate. Use the animation property and set it to play only when the element is in the viewport.

.scroll-animation {
  opacity: 0; /* Start invisible */
  animation: fadeIn 1s ease-out forwards;
  animation-play-state: paused; /* Pause by default */
}
Enter fullscreen mode Exit fullscreen mode

Step 3: Trigger the Animation on Scroll with @scroll-timeline (Experimental)

CSS now supports scroll-triggered animations using the @scroll-timeline rule (currently experimental and supported in some browsers like Chrome). Here’s how you can use it:

@scroll-timeline fadeInTimeline {
  source: auto;
  orientation: vertical;
  scroll-offsets: 0%, 100%;
}

.scroll-animation {
  opacity: 0;
  animation: fadeIn 1s ease-out forwards;
  animation-timeline: fadeInTimeline;
}
Enter fullscreen mode Exit fullscreen mode

Step 4: Fallback for Browsers Without @scroll-timeline Support

For browsers that don’t support @scroll-timeline, you can use JavaScript as a fallback. Here’s a simple way to do it:

const elements = document.querySelectorAll('.scroll-animation');

const observer = new IntersectionObserver((entries) => {
  entries.forEach(entry => {
    if (entry.isIntersecting) {
      entry.target.style.animationPlayState = 'running';
    }
  });
});

elements.forEach(element => {
  observer.observe(element);
});
Enter fullscreen mode Exit fullscreen mode

Pro Tips for Scroll-Triggered Animations

  1. Keep It Subtle: Avoid overloading your site with animations. Less is more!
  2. Optimize Performance: Use will-change to hint the browser about animations for smoother performance.
  3. Test Across Browsers: Ensure your animations work well on all major browsers.

Example Code

Here’s a complete example:

<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <style>
    @keyframes fadeIn {
      from { opacity: 0; transform: translateY(20px); }
      to { opacity: 1; transform: translateY(0); }
    }

    .scroll-animation {
      opacity: 0;
      animation: fadeIn 1s ease-out forwards;
      animation-play-state: paused;
    }

    @scroll-timeline fadeInTimeline {
      source: auto;
      orientation: vertical;
      scroll-offsets: 0%, 100%;
    }

    .scroll-animation {
      animation-timeline: fadeInTimeline;
    }
  </style>
</head>
<body>
  <div class="scroll-animation">
    <h1>Hello, World!</h1>
  </div>
  <div style="height: 1000px;"></div> <!-- Spacer for scrolling -->
</body>
</html>
Enter fullscreen mode Exit fullscreen mode

Why Use Scroll-Triggered Animations?

  • Engage Users: Animations grab attention and keep users interested.
  • Improve UX: They guide users through your content naturally.
  • Stand Out: A well-animated site feels modern and professional.

Give it a try and let me know how it goes! πŸš€ If you have any questions or cool examples of scroll-triggered animations, drop them in the comments below. Let’s make the web more dynamic together! πŸ’»βœ¨


Like this post? Share it with your fellow devs and spread the animation love! ❀️

Top comments (0)