Jump to content
Search Community

reversing in react with gsap

daryl.codes test
Moderator Tag

Recommended Posts

Hello, I have 3 simple menu bars i am animating but id like to now reverse, i know about reverse() but i cant seem to implement it correctly.

 

const [state, setState] = useState({
    active: null
  })

const handleClick = () => {
    setState({
    active: !state.active
})

  }

let bar1, bar2, bar3 = useRef(null)

useEffect(()=>{
    if(state.active === true){
        gsap.to(bar1, 0.4, { x: "+=80px", y: "-=80px", delay: 0.1, ease: Power4.easeIn })
        gsap.to(bar2, 0.4, {  x: "+=80px", y: "-=80px", ease: Power4.easeIn})
        gsap.to(bar3, 0.4, { x: "+=80px", y: "-=80px", delay: 0.2, ease: Power4.easeIn})
    }else if (state.active.reversed()){
      gsap.to(bar1, 0.4, { x: "+=80px", y: "-=80px", delay: 0.1, ease: Power4.easeIn }).reverse()
        gsap.to(bar2, 0.4, {  x: "+=80px", y: "-=80px", ease: Power4.easeIn}).reverse()
        gsap.to(bar3, 0.4, { x: "+=80px", y: "-=80px", delay: 0.2, ease: Power4.easeIn}).reverse()
    }
})

Help needed!

https://codesandbox.io/s/musing-christian-ncvot?file=/src/components/atom/Menu.js

Link to comment
Share on other sites

8 hours ago, daryl.codes said:

I heard that gsap should not be used with css is that true?

 

No, just that you shouldn't animate the same properties with gsap and css. For example, if you have transition: all 1s; css on an element and then try to animate that element with gsap, it's probably going to animate incorrectly.

 

 

  • Like 2
Link to comment
Share on other sites

Hey Blake I have been working on a new section of the page. And need help animating a few text.

function Blob() {
  let animatedEl = useRef(null)
  let animatedEl2 = useRef(null)
  let animatedEl3 = useRef(null)
  let animatedEl4 = useRef(null)

  useEffect(()=>{
    TweenMax.staggerFrom(1, {y: 100}, .5);
    })

return (
  
    <div className="blobDiv">    
    
      <Menu />
      <div className="blob">
        <h6 ref={(el) => {animatedEl = el}}>HI THERE !</h6>
        <h1 ref={(el) => {animatedEl2 = el}}><span>I'M</span> DARYL DAUPHIN</h1>
        <p ref={(el) => {animatedEl3 = el}}>
          I'm a American based frontend developer focused on crafting clean & userfriendly experiences, I am passionate about building excellent software that improves the lives of those around me.
        </p>
      </div>   

      <Button ref={(el) => {animatedEl4 = el}} />
    </div>
  );
}

export default Blob;

my useeffect needs some work, can you help?

Link to comment
Share on other sites

A useEffect should always have an array as the second parameter, and your gsap code  doesn't have a valid target. You should also stick to the new syntax. TweenLite, TweenMax, TimelineLite, and TimelineMax are deprecated.

 

If you're trying to stagger all 4 elements, then it might be something like this.

 

export default function() {
  let animatedEl = useRef(null)
  let animatedEl2 = useRef(null)
  let animatedEl3 = useRef(null)
  let animatedEl4 = useRef(null)

  useEffect(()=>{
    
    const staggerTargets = [
      animatedEl.current,
      animatedEl2.current,
      animatedEl3.current,
      animatedEl4.current
    ];
    
    const animation = gsap.from(staggerTargets, {
      y: 100,
      stagger: 0.5
    });
    
    // kill animation on unmount
    return () => animation.kill()
    
  }, []);

return (
  
    <div className="blobDiv">    
    
      <Menu />
      <div className="blob">
        <h6 ref={animatedEl}>HI THERE !</h6>
        <h1 ref={animatedEl2}><span>I'M</span> DARYL DAUPHIN</h1>
        <p ref={animatedEl3}>
          I'm a American based frontend developer focused on crafting clean & userfriendly experiences, I am passionate about building excellent software that improves the lives of those around me.
        </p>
      </div>   

      <Button ref={animatedEl4} />
    </div>
  );
}

 

 

  • Like 1
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...