Lädt...


🔧 Pinning Images with GSAP: A Smooth Scrolling Animation in Next.js


Nachrichtenbereich: 🔧 Programmierung
🔗 Quelle: dev.to

In this article, we’ll explore how to create smooth scrolling animations by pinning images using GSAP (GreenSock Animation Platform) and its ScrollTrigger plugin. This technique enhances user experience by creating dynamic effects as users scroll through your content. We’ll use Next.js and Tailwind CSS for styling, ensuring a modern look for our project.

Table of Contents

  • Demo
  • Installation
  • Implementation
  • Conclusion

Demo

Before jumping to the explanation check out the live demo of our pinning images animation on CodeSandbox or Loom

Installation

To get started, make sure you have a Next.js application set up. (Follow Next.js Installation Documentation: https://nextjs.org/docs/getting-started/installation)

Next, Tailwind CSS: (Follow tailwind Installation Documentation/Install with next js)

Next, install the necessary packages for GSAP:
npm install gsap @gsap/react

Implementation

Now, let’s create the ScrollAnimation component that will handle our pinning images animation. Here’s the complete code:

"use client";
import { useRef } from "react";
import { gsap } from "gsap";
import { ScrollTrigger } from "gsap/ScrollTrigger";
import { useGSAP } from "@gsap/react";

gsap.registerPlugin(ScrollTrigger); // Register Gsap Scroll Trigger Plugin

// I used freepik images link for this purpose
const imageUrls = [
  "https://img.freepik.com/free-photo/wide-angle-shot-single-tree-growing-clouded-sky-sunset-surrounded-by-grass_181624-22807.jpg?ga=GA1.1.2030075610.1697991259&semt=ais_hybrid-rr-similar",
  "https://img.freepik.com/free-photo/cascade-boat-clean-china-natural-rural_1417-1356.jpg?ga=GA1.1.2030075610.1697991259&semt=ais_hybrid-rr-similar",
  "https://img.freepik.com/free-photo/group-elephants-big-green-tree-wilderness_181624-16897.jpg?ga=GA1.1.2030075610.1697991259&semt=ais_hybrid-rr-similar",
  "https://img.freepik.com/premium-photo/blazing-sun-vast-savanna_1272857-120118.jpg?ga=GA1.1.2030075610.1697991259&semt=ais_hybrid-rr-similar",
  "https://img.freepik.com/free-photo/beautiful-shot-tree-savanna-plains-with-blue-sky_181624-21992.jpg?ga=GA1.1.2030075610.1697991259&semt=ais_hybrid-rr-similar",
];

const ScrollAnimation = () => {
  const imagesRef = useRef<any[]>([]); // Fix to store multiple refs

  useGSAP(() => {
    // Convert the current array of image references to a proper array for GSAP manipulation
    const images = gsap.utils.toArray(imagesRef.current);

    // Iterate over each image element
    images.forEach((panel: any, i: number) => {
      let scale = 1;

      // If the current image is not the last one, adjust the scale based on its index
      if (i !== images.length - 1) {
        scale = 0.9 + 0.025 * i; // Create a slight scaling effect for images based on their index
      }

      gsap.to(panel, {
        scale: scale,
        transformOrigin: "top center", // Specify the point from which the scaling transformation occurs
        ease: "none",

        // Configure the ScrollTrigger plugin for scroll-based animations
        scrollTrigger: {
          trigger: panel, // Set the current image as the trigger for the ScrollTrigger

          // Define when the animation should start based on the position of the trigger
          start: "top " + (70 + 40 * i), // Start the animation when the top of the panel is 70px down plus an offset based on index
          end: "bottom +=650px", // Define when the animation should end (bottom of the panel + 650px)
          endTrigger: ".end", // Specify the end trigger element
          pin: true, // Pin the current panel/image in place while it is being triggered
          pinSpacing: false, // Disable additional spacing around pinned elements
          scrub: true, // Enable scrub for smooth animation with scrolling
        },
      });
    });
  }, []);

  return (
    <div className="flex flex-col gap-12 mx-auto max-w-2xl py-12">
      {imageUrls.map((image, index) => (
        <div
          key={index}
          ref={(el) => (imagesRef.current[index] = el)}
          className=""
        >
          <img
            src={image}
            alt={`Image ${index + 1}`}
            className="w-full h-auto object-cover rounded-lg shadow-lg"
          />
        </div>
      ))}
      <div className="end"></div>
    </div>
  );
};

export default ScrollAnimation;

Understanding the Code

Image References: We create an array of references using useRef to manipulate our image elements directly.
GSAP Animations: Inside the useEffect, we loop through each image and set a scale based on its index. This creates a dynamic scaling effect as the user scrolls.
ScrollTrigger Configuration: We configure ScrollTrigger for each image, specifying when the animation starts and ends, pinning the images in place while scrolling.

Using the Component

To include the ScrollAnimation component in your Next.js application, import it into your main application file (e.g., page.tsx):

import ScrollAnimation from "./ScrollAnimation";

export default function Home() {
  return (
    <main>
      <div className="h-screen w-full flex items-center justify-center bg-[#66545e]">
        <h1 className="text-2xl font-bold">Scroll Down To See The Magic</h1>
      </div>
      {/* The ScrollAnimation Component */}
      <ScrollAnimation />
      <div className="bg-[#aa6f73] h-screen w-full flex items-center justify-center">
        <h1 className="text-2xl font-bold">Pinning Image Animation End</h1>
      </div>
    </main>
  );
}

Now run the application and boom. 💥 💥

Here is the sandbox link again: CodeSandbox

Conclusion

You now know how to pin images and create smooth scrolling animations using GSAP and ScrollTrigger in a Next.js application! This technique can greatly enhance the visual appeal of your site. Feel free to customize the images and scaling effects to match your project’s style.

If you found this article helpful, please share it on social media and leave a comment below. Happy coding! 💻😃

...

🔧 Pinning Images with GSAP: A Smooth Scrolling Animation in Next.js


📈 94.49 Punkte
🔧 Programmierung

🐧 What is your opinion on smooth scrolling vs traditional non smooth scrolling on your favorite apps?


📈 61.63 Punkte
🐧 Linux Tipps

🔧 Implement Smooth Scrolling & Parallax Effect in Next.js using Lenis and GSAP


📈 58.67 Punkte
🔧 Programmierung

🔧 Image Reveal Animation with HTML, CSS, and GSAP


📈 34.73 Punkte
🔧 Programmierung

🔧 Fun Animation using GSAP


📈 34.73 Punkte
🔧 Programmierung

🔧 Cricket League Registration Landing Page with GSAP Animation


📈 34.73 Punkte
🔧 Programmierung

🔧 GSAP vs. Framer Motion: Which Animation Library Should You Choose for Your Creative Web Projects?


📈 34.73 Punkte
🔧 Programmierung

🔧 SVG path animation in Nextjs using GSAP


📈 34.73 Punkte
🔧 Programmierung

🎥 React 3D Animation Website Tutorial with ThreeJS (WebGi) & GSAP


📈 34.73 Punkte
🎥 Video | Youtube

📰 How to get Edge like smooth scrolling in Chrome on Windows 10


📈 30.81 Punkte
🖥️ Betriebssysteme

📰 How to get Edge like smooth scrolling in Chrome on Windows 10


📈 30.81 Punkte
🖥️ Betriebssysteme

🔧 Implementing Smooth Scrolling for a Better User Experience.


📈 30.81 Punkte
🔧 Programmierung

🔧 How To AutoScroll In React?Creating Smooth Auto-Scrolling Functionality in React


📈 30.81 Punkte
🔧 Programmierung

🔧 #CodePenChallenge: Smooth Scrolling


📈 30.81 Punkte
🔧 Programmierung

🐧 Linux Touchpad like Macbook Update: 2023 Progress on Smooth Scrolling


📈 30.81 Punkte
🐧 Linux Tipps

🐧 No smooth scrolling in Steam


📈 30.81 Punkte
🐧 Linux Tipps

📰 How to enable smooth scrolling in Firefox


📈 30.81 Punkte
📰 IT Nachrichten

🐧 Is there a distro where the touchpad scrolling works fine and smooth?


📈 30.81 Punkte
🐧 Linux Tipps

🐧 Smooth scrolling with custom device


📈 30.81 Punkte
🐧 Linux Tipps

🐧 Smooth scrolling


📈 30.81 Punkte
🐧 Linux Tipps

🪟 How to get Edge like smooth scrolling in Chrome on Windows 10


📈 30.81 Punkte
🪟 Windows Tipps

🕵️ Smooth Slider Plugin up to 2.8.6 on WordPress smooth-slider.php trid sql injection


📈 30.72 Punkte
🕵️ Sicherheitslücken

🕵️ Smooth Slider Plugin bis 2.8.6 auf WordPress smooth-slider.php trid SQL Injection


📈 30.72 Punkte
🕵️ Sicherheitslücken

🔧 Creating a Scrolling Logo Animation with Tailwind CSS


📈 27.67 Punkte
🔧 Programmierung

🔧 Creating a Scrolling Animation for Apple AirPods


📈 27.67 Punkte
🔧 Programmierung

🔧 Creating a Scrolling Animation for Apple AirPods


📈 27.67 Punkte
🔧 Programmierung

📰 How to Enable Microsoft’s Own Scrolling Animation in Google Chrome


📈 27.67 Punkte
📰 IT Security Nachrichten

🔧 iOS style scrolling dock with scroll-driven animation 🤔


📈 27.67 Punkte
🔧 Programmierung

🔧 👨‍💻Building a Modern AI-Themed Landing Page with Responsive Design and Smooth Animation


📈 27.58 Punkte
🔧 Programmierung

🔧 Cursor Animation : Creating Smooth Hover Effects in CSS


📈 27.58 Punkte
🔧 Programmierung

🐧 Is there a terminal emulator with smooth cursor animation?


📈 27.58 Punkte
🐧 Linux Tipps

🔧 Rendering( or How to Render) Animation in JSON format with LottieFiles animation in React application


📈 24.44 Punkte
🔧 Programmierung

🔧 Animation -22 : Cube Flipping Loader CSS Animation


📈 24.44 Punkte
🔧 Programmierung

matomo