Jump to content
Search Community

GSAP between multiple pages

ovlack
Moderator Tag

Recommended Posts

Posted

Hi everyone, I am quite new to JavaScript and GSAP. I'm currently developing a new personal site, with Bootstrap, GSAP and Lenis. I've split up my few JavaScript files up now. 

 

this is a snippet of gsap.js - it currently contains all the relevant GSAP animations throughout the website. Most of these are on my "index" page, with a few (not included) on my "about" page. When I navigate to the "about" page, a lot of GSAP warnings are logged about an element not being found, that's okay, makes sense. I imagine most people won't see that unless they know about browser dev tools. Are there optimal ways to split up page-specific animations? Is it "easier" to keep it one file? I understand splitting them up too. What I would like to do is consolidate some selectors into general ones cause I plan to use the same simple fade animation on most elements. 

// gsapAnimations.js
import { gsap } from "gsap"
import { ScrollTrigger } from "gsap/ScrollTrigger"
 
// Register ScrollTrigger with GSAP
gsap.registerPlugin(ScrollTrigger)
 
// Refresh ScrollTrigger on load and resize
window.addEventListener("load", () => ScrollTrigger.refresh())
window.addEventListener("resize", () => ScrollTrigger.refresh())
 
// Function to initialize GSAP animations
export function initGSAPAnimations() {
  console.log("Initializing GSAP animations...")
  // Example animation
  gsap.from([".hero__landing", ".img__hero"], {
    y: 100,
    opacity: 0,
    duration: 1,
    ease: "power2.out",
    scrollTrigger: {
      trigger: ".hero__landing",
      start: "top bottom",
      end: "top top",
      scrub: false,
      markers: false,
      toggleActions: "play none none reverse",
    },
  })
 
  gsap.utils.toArray(".row-stacked-1").forEach((row) => {
    const leftCol = row.querySelector(".col-stacked-left") // Target the left column in this row
 
    ScrollTrigger.create({
      trigger: row, // The row-stacked-1 acts as the trigger
      start: "top center", // Adjust starting point
      end: "bottom center", // Adjust ending point
      pin: leftCol, // Pin the left column within this row
      pinSpacing: true, // Keep spacing to avoid layout shift
      markers: false, // Debugging (remove in production)
    })
  })
 
  // Animate the header and text of wrapper__experience
  gsap.from(".wrapper__about-intro h2, .wrapper__about-intro p", {
    scrollTrigger: {
      trigger: "#section-projects-wrapper", // Triggered by the projects section
      start: "bottom 75%", // When the bottom of projects hits the bottom of the viewport
      markers: false, // Debug markers (remove in production)
      toggleActions: "play none none reverse",
    },
    opacity: 0,
    y: 30,
    duration: 1,
    ease: "power2.out",
    stagger: 0.3, // Stagger for header and paragraph
  })
 
  // Animate the list items in the experience section
  gsap.utils.toArray(".list-group-item").forEach((item, i) => {
    gsap.from(item, {
      scrollTrigger: {
        trigger: "#section-projects-wrapper", // Triggered by the projects section
        start: "bottom 75%", // When the bottom of projects hits the bottom of the viewport
        markers: false, // Optional debug markers
        toggleActions: "play none none reverse",
      },
      opacity: 0,
      y: 20,
      duration: 0.8,
      ease: "power2.out",
      delay: i * 0.2, // Staggered effect for list items
    })
  })
}
Posted

Without a minimal demo, it's very difficult to troubleshoot; the issue could be caused by CSS, markup, a third party library, a 3rd party script, etc. Would you please provide a very simple CodePen or Stackblitz that illustrates the issue? 

 

Please don't include your whole project. Just some colored <div> elements and the GSAP code is best. See if you can recreate the issue with as few dependencies as possible. Start minimal and then incrementally add code bit by bit until it breaks. Usually people solve their own issues during this process! If not, at least we have a reduced test case which greatly increases your chances of getting a relevant answer.

 

See the Pen aYYOdN by GreenSock (@GreenSock) on CodePen.

that loads all the plugins. Just click "fork" at the bottom right and make your minimal demo

 

Using a framework/library like React, Vue, Next, etc.? 

CodePen isn't always ideal for these tools, so here are some Stackblitz starter templates that you can fork and import the gsap-trial NPM package for using any of the bonus plugins: 

 

Please share the StackBlitz link directly to the file in question (where you've put the GSAP code) so we don't need to hunt through all the files. 

 

Once we see an isolated demo, we'll do our best to jump in and help with your GSAP-specific questions. 

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...