Hi, I've started with GSAP recently and I'm working on a project and the click event animation works fine first 2-3 times but then it starts delaying the animation for more than 6-7 seconds. I'm trying to not duplicate the menu node so I'm trying to animate the same menu differently on mobile and desktop. I'll attach some screenshots below for better understanding. I can't provide codepen because it's a WordPress project, I'm not using any builder plugin. The menu is supposed to be closed by default and on resize on mobile while appearing on the right side. And on desktop it's supposed to be open on the left by default and on resize.
gsap.registerPlugin(ScrollTrigger);
document.addEventListener('DOMContentLoaded', function(){
let navTl = gsap.timeline({ paused: true, yoyo:true, reversed: true });
const header = {
main: document.querySelector('.site-header'),
leftNav: document.querySelector('.leftNavWrap'),
button: document.querySelector('.siteToggler button'),
sections: document.querySelector('.marginWrapper')
}
function debounce(func){
let timer;
return function(event){
if(timer) clearTimeout(timer);
timer = setTimeout(func, 100, event);
};
}
function leftNavPosition(func){
const headerHeight = header.main.offsetHeight;
header.leftNav.style.top = headerHeight + 'px';
func();
}
function updateNavOnResize(){
let isMobile = window.matchMedia('(max-width: 991px)');
if(isMobile.matches){
gsap.to('.leftNavWrap', {x: '100%', duration: 0});
} else{
gsap.to('.leftNavWrap', {x: '0', duration: 0});
}
}
window.addEventListener('resize', debounce(function(e){
leftNavPosition(updateNavOnResize);
}));
leftNavPosition(updateNavOnResize);
function toggleNav(){
let mq = gsap.matchMedia();
mq.add({
isMobile: '(max-width: 991px)',
isDesktop: '(min-width: 992px)'
}, (context)=>{
let {isMobile, isDesktop} = context.conditions;
navTl.to('.leftNavWrap', {
duration: 1,
x: isMobile ? '0' : '-100%' ,
ease: Expo.easeInOut,
});
});
navTl.reversed() ? navTl.play() : navTl.reverse();
}
const button = document.querySelector('.siteToggler button');
button.addEventListener('click', function(){
const currentState = button.getAttribute("data-state");
if (!currentState || currentState === "closed") {
button.setAttribute("data-state", "opened");
button.setAttribute("aria-expanded", "true");
}
else {
button.setAttribute("data-state", "closed");
button.setAttribute("aria-expanded", "false");
}
if(!navTl.isActive()){toggleNav();}
});
});