Search code examples
javascriptreactjsgsap

GSAP - reverse() don't animate back


The animation works when it is played but does not work when I use reverse() function

It's demo https://codesandbox.io/s/gatsby-express-9uqi7 Navbar component

const [state, setState] = useState(false);
    const menuManage = (e)=> {
        if(!e.target.classList.contains('select')){
            const wrapper = document.querySelector('.wrapper');
            const tl = new TimelineMax({paused: true})
                .to(wrapper, 0.15, {height: '100vh', width: '100vw', left:0})
                .to(wrapper, 0.15, {borderBottomLeftRadius: '0%'});
            if(state){
                tl.reverse();
                setState(false);
            }
            else{
                tl.play();
                setState(true);
            }
        }
    }

Solution

  • I'm guessing that this is because the timeline's playhead is at the beginning of the timeline when you're calling .reverse(). Try tl.progress(1).reverse(); instead. Without a demo, it's impossible to tell if this is the issue.