Sup,
I am using gsap with ScrollTrigger to toggle dark/light mode on my site.
I wrote a function to detect when a trigger become visible:
ScrollTrigger.create({
trigger: ".js__trigger-dark",
start: "bottom bottom",
end: "+=300",
onLeave: self => changeTheme(),
onEnterBack: self => disableTheme(),
});
ScrollTrigger.create({
trigger: ".js__trigger-light",
start: 'bottom bottom',
onLeave: self => disableTheme(),
onEnterBack: self => changeTheme(),
});
function changeTheme() {
jQuery('html').attr('data-theme','dark');
}
function disableTheme() {
jQuery('html').attr('data-theme','');
}
For example .js__trigger-dark toggles dark mode when the user scrolls 300px past the first section and removes the attribute when the user scrolls up again into the section with the class .js__trigger-dark. This works fine until the user reaches the bottom of the screen and the attribute is removed.
Can someone explain why it gets removed when scrolling to the bottom of the screen? For some reason onEnterBack is being called and I dont know why.
Regards, b