Jump to content
GreenSock

Search In
  • More options...
Find results that contain...
Find results in...
Shutt90

Closing all other elements

Go to solution Solved by OSUblake,

Recommended Posts

Not sure if this is considered a GSAP question tbh but I'm looking to close all other rotations in the loop when that's already open is pressed using vanilla JS. Becuause I'm in a foreach I'm not sure on the best way or if theres a special gsap way to do it. TIA

 

See the Pen wvqyEyb by shutt90 (@shutt90) on CodePen

  • Like 2
Link to comment
Share on other sites

  • Solution

Hi Shutt,

 

Do you mean something like this?

 

See the Pen oNeEPKM by GreenSock (@GreenSock) on CodePen

 

  • Like 6
Link to comment
Share on other sites

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

Both are excellent solutions thank you.

Just having a slight issue getting it working with my full code.

 

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.
×