I don't know if you are familiar with Swup, I quote the website "Swup is an extensible and easy-to-use page transition library for server-side rendered websites. It handles the complete lifecycle of a page visit by intercepting link clicks, loading the new page in the background, replacing the content and transitioning between the old and the new page."
This means that if I define some scrolltriggers (indipendently or inside gsap.matchMedia()), when I click to go to another page, those "entities" are not destroyed by the reloading of the page, because there's no reloading between pages.
So I have to kill everything I initialized in the old page and initialize what I need in the new page, using the events provided by Swup: https://swup.js.org/getting-started/reloading-javascript/
What happened to me is that I easily killed the scrolltriggers using ScrollTrigger.getAll(), but the scrolltriggers created inside gsap.matchMedia() were being recreated when the mediaquery matched, even if they were killed before.
My code (which is called every time a new page "loads") was something like this:
let mm = gsap.matchMedia();
if ("some conditions are met")
{
mm.add({
isDesktop: '(min-width: 992px)',
isMobile: '(max-width: 767.98px)'
}, (context) => {
let {isDesktop, isMobile} = context.conditions;
// create scrolltriggers
});
}
else
{
mm.revert();
}
When the conditions were not met, mm.revert(); was called but the scrolltriggers were recreated on resize anyway.
This is what I don't understand: mm.revert() was called but the "old" mm somehow survived.
I solved my problem like this:
let mm = gsap.matchMedia();
mm.add({
isDesktop: '(min-width: 992px)',
isMobile: '(max-width: 767.98px)'
}, (context) => {
if ("some conditions are met")
{
let {isDesktop, isMobile} = context.conditions;
// create scrolltriggers
else
{
mm.revert();
}
});
}
I don't know if this is the right strategy, but I moved the "if" inside the context.
Now it works but I don't know why