Jump to content
Search Community

Closing all other elements

Shutt90 test
Moderator Tag

Go to solution Solved by OSUblake,

Recommended Posts

The solution from @OSUblake should be all you need. 👍

 

Here's a slightly different approach I had laying around in case it helps.

 

Instead of looping through all the animations to see which one to reverse I keep track of the currently open (or active) item with an active variable. Every time you open something you check to see if there is an "active" element. If yes, close it by reversing its animation and then update the active variable to reference the thing you just clicked on. In this example there is a dedicated close button for each element (which you may not need).

 

See the Pen YzGpXWO by snorkltv (@snorkltv) on CodePen

  • Like 5
Link to comment
Share on other sites

So I'm using the same code and everything should be set up correctly but I keep getting 

 

Invalid property rotate set to 45 Missing plugin? gsap.registerPlugin()

 

I'm importing GSAP from CDN, with CSSRule and ScrollTrigger at top of file with my custom js file on defer, I also have other gsap further down the page with rotations thats working fine with rotate. Also I had added the gsap.to(anim.... for debugging purposes so I think this could be the issue for it not working.

 

EDIT - managed to get it working, I was trying to rotate an svg/i from font awesome and it wasn't liking it so had to target the outer div

 

const skills = gsap.utils.toArray('.skills-container__inner');
 
const animations = skills.map(element => {
    const animation = gsap.to(element.querySelector(".skills-rotate"), {
      rotate: 45,
      reversed: true
    });
    console.log(element.querySelector('.skills-rotate'))
 
        element.addEventListener("click", () => {
       
            animations.forEach(anim => {      
            if (anim === animation) {    
                gsap.to(anim, {
                    rotate: 45,
                })    
                // toggle this animation
                anim.reversed(!anim.reversed());
            } else {
               
                // reverse all the other animations
                anim.reverse();
            }      
            });
        });
       
        return animation;
        });
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...