Jump to content
Search Community

GSAP animation not starting as expected on next js. It's take element bottom as top.

Alif Ahmed test
Moderator Tag

Recommended Posts

The animtion should be start on when image top hit on viewport bottom but when I enabled markers then I see gsap taking image bottom as top so it's making problem like it's starting too late.
 
image.thumb.png.3b7b64219ff7dc99a00c23f3ce296258.png




'use client'
import Image from "next/image";
import "./process.scss";
import CalendarMonthIcon from '@mui/icons-material/CalendarMonth';
import StartIcon from '@mui/icons-material/Start';
import {useRef, useEffect} from 'react';
import gsap from 'gsap';
import { ScrollTrigger } from "gsap/dist/ScrollTrigger";
gsap.registerPlugin(ScrollTrigger);



 
export default function ProcessSection() {
  const root1 = useRef();
  const root2 = useRef();
  const root3 = useRef();
 
  const processArticleImage1 = useRef();
  const processArticleImage2 = useRef();
  const processArticleImage3 = useRef();
 
  const processArticleHeading1 = useRef();
  const processArticleHeading2 = useRef();
  const processArticleHeading3 = useRef();
 
  const process1Desc1 = useRef();
  const process1Desc2 = useRef();
  const process2Desc1 = useRef();
  const process2Desc2 = useRef();
  const process3Desc1 = useRef();
  const process3Desc2 = useRef();
 
  function animateElement({
    triggerElement,
 
    scrub = .2,
    sx,
    x,
    opacity = 1,
  }) {
    gsap.fromTo(
      triggerElement,
      {
        x: sx,
        opacity: 0,
      },
      {
        scrollTrigger: {
          trigger: triggerElement,
          start: 'top  bottom',
          end: 'center center',
          scrub: true,
          invalidateOnRefresh: true,
          markers: true
        },
        x: x,
        opacity: opacity,
      }
    );
  }
 
  useEffect(()=>{
    animateElement({
      triggerElement: '.processArticleImage1',
      sx: -300,
      x: 0,
    });
  },[])
 
 
   
 
  return (
    <section className="processSection" id="ourprocess" >
      <div className="bodyContainer">
        <div className="processSectionWrapper" >
          <div className="processSectionHeading">
            <h1>How We Work!</h1>
          </div>
          <div className="processWrapper container" >
          <div className="process" ref={root1}>
            <article  className='singleProcess' >
              <Image ref={processArticleImage1} className="processArticleImage1"  src="/process/meeting.svg" width={50} height={50} alt="how dsyncro work"/>
              <div className="processContent">
                <div className="contentHeading">
                  <h2>Project Discovery and Planning</h2>
                </div>
                <div className="contentBody">
                  <p><span>Embark on a Creative Odyssey:</span> Dive deep into the client&apos;s vision through spirited initial meetings. Uncover the essence of their project goals, painting a vivid picture of the journey ahead.</p>
                  <p><span>Blueprints to Brilliance:</span>  Bring plans to life! Navigate the digital landscape with meticulous project planning, where wireframes and mockups sculpt the first strokes of your masterpiece..</p>
                </div>
              </div>
            </article>
          </div>
          <div className="process">
            <article>
              <div className="processContent">
                <div className="contentHeading">
                  <h2>Project Discovery and Planning</h2>
                </div>
                <div className="contentBody">
                  <p><span>Embark on a Creative Odyssey:</span> Dive deep into the client&apos;s vision through spirited initial meetings. Uncover the essence of their project goals, painting a vivid picture of the journey ahead.</p>
                  <p><span>Blueprints to Brilliance:</span>  Bring plans to life! Navigate the digital landscape with meticulous project planning, where wireframes and mockups sculpt the first strokes of your masterpiece..</p>
                </div>
              </div>
              <Image src="/process/development.svg" width={50} height={50} alt='How dsyncro development'/>
            </article>
          </div>
          <div className="process">
            <article>
              <Image src="/process/meeting.svg" width={50} height={50} alt="how dsyncro work"/>
              <div className="processContent">
                <div className="contentHeading">
                  <h2>Project Discovery and Planning</h2>
                </div>
                <div className="contentBody">
                  <p><span>Embark on a Creative Odyssey:</span> Dive deep into the client&apos;s vision through spirited initial meetings. Uncover the essence of their project goals, painting a vivid picture of the journey ahead.</p>
                  <p><span>Blueprints to Brilliance:</span>  Bring plans to life! Navigate the digital landscape with meticulous project planning, where wireframes and mockups sculpt the first strokes of your masterpiece..</p>
                </div>
              </div>
            </article>
          </div>
          </div>
          <div className="processSectoionFooter">
              <span className="CTA-Qoute">
                Get a Qoute
                <StartIcon />
                </span>
              <span className="CTA_Schedule">
                <CalendarMonthIcon />
                Schedule a Call</span>
            </div>
        </div>
        </div>
    </section>
  );
}

image.png

Link to comment
Share on other sites

It's pretty tough to troubleshoot without a minimal demo - the issue could be caused by CSS, markup, a third party library, your browser, an external script that's totally unrelated to GSAP, etc. Would you please provide a very simple CodePen or Stackblitz that demonstrates 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 dependancies as possible. If not, incrementally add code bit by bit until it breaks. Usually people solve their own issues during this process! If not, then at least we have a reduced test case which greatly increases your chances of getting a relevant answer.

 

Here's a starter CodePen that loads all the plugins. Just click "fork" at the bottom right and make your minimal demo

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

 

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. 

Link to comment
Share on other sites

Hi there! I see you're using React -

Proper animation cleanup is very important with frameworks, but especially with React. React 18 runs in strict mode locally by default which causes your useEffect() and useLayoutEffect() to get called TWICE.

In GSAP 3.11, we introduced a new gsap.context() feature that helps make animation cleanup a breeze. All you need to do is wrap your code in a context call. All GSAP animations and ScrollTriggers created within the function get collected up in that context so that you can easily revert() ALL of them at once.

Here's the structure:

// typically it's best to useLayoutEffect() instead of useEffect() to have React render the initial state properly from the very start.
useLayoutEffect(() => {
  let ctx = gsap.context(() => {
    // all your GSAP animation code here
  });
  return () => ctx.revert(); // <- cleanup!
}, []);

This pattern follows React's best practices, and one of the React team members chimed in here if you'd like more background.

We strongly recommend reading the React information we've put together at https://gsap.com/resources/React/

Happy tweening!

Link to comment
Share on other sites

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...