Hello!
To open a menu, I use a timeline like below:
menuTl
.set($mainMenu, {
opacity: 1,
pointerEvents: "auto"
})
.to($mainMenu, 0.5, {
width: '100%',
opacity: 1,
ease: "power1.easeOut"
})
.staggerFromTo($mainMenuItems, .3, {
x: -50,
opacity: 0,
ease: "back.out(1)"
}, {
x: 0,
opacity: 1
}, .2)
.fromTo($btnCloseMenu, 0.8,{
translateX: 200,
}, {
opacity: 1,
pointerEvents: "auto",
translateX: 0,
});
If you click on the menu close button, I reverse the timeline as follows:
$btnCloseMenu.on('click', (evt) => {
evt.preventDefault();
document.body.classList.remove('menu-open');
if(!menuTl.isActive()) {
menuTl.timeScale(2).reverse();
}
});
Now when you click on a menu item (all sections are on the homepage), I would like to reverse the timeline and then when completed, execute a scrollTo the specified anchor. This is what I have done but it's not working:
document.querySelectorAll('.menu-link').forEach((link, index) => {
link.addEventListener('click', (evt) => {
evt.preventDefault();
const target = link.dataset.target;
document.body.classList.remove('menu-open');
if(!menuTl.isActive()) {
menuTl.timeScale(2).reverse();
gsap.to(window, {duration: 1, scrollTo:"#" + target});
}
});
});
The scrollTo is executed in parallel with the reverse.
Thx for your help!