Shutt90 Posted November 5, 2021 Share Posted November 5, 2021 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 2 Link to comment Share on other sites More sharing options...
Solution OSUblake Posted November 5, 2021 Solution Share Posted November 5, 2021 Hi Shutt, Do you mean something like this? See the Pen oNeEPKM by GreenSock (@GreenSock) on CodePen 6 Link to comment Share on other sites More sharing options...
Carl Posted November 5, 2021 Share Posted November 5, 2021 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 5 Link to comment Share on other sites More sharing options...
Shutt90 Posted November 5, 2021 Author Share Posted November 5, 2021 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 More sharing options...
Shutt90 Posted November 8, 2021 Author Share Posted November 8, 2021 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 More sharing options...
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now