Jump to content
Search Community

ScrollTrigger toggleClass: multiple class names.

egrandinetti test
Moderator Tag

Recommended Posts

I'm trying to assign multiple class names to toggleClass parameter (space separated), but I noticed that, because of the classList.toggle method, it's impossibile. There's a way to avoid using a combination of jQuery and onEnter/onLeaveBack methods to get the same goal?

Link to comment
Share on other sites

Hey egrandinetti. At the moment toggleClass only supports a single class to be provided. You can toggle multiple classes by writing your own logic. Here's an example:

const elem = document.querySelector(".myElem");
const classes = ["test", "test2"];
function toggleClasses() {
  classes.forEach(myClass => elem.classList.toggle(myClass) )
}
gsap.to(elem, {
  scrollTrigger: {
    trigger: elem,
    onEnter: toggleClasses,
    onLeaveBack: toggleClasses,
    onLeave: toggleClasses,
    onEnterBack: toggleClasses
  },
  // ...
});

 

  • Thanks 1
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...