Jump to content
GreenSock

Search In
  • More options...
Find results that contain...
Find results in...
Prsatut Dahal

GSAP Animations trigger only on Click

Recommended Posts

So I've just started learning GSAP and I recently faced a problem with something that I'm trying. So I have multiple div's with the same style as height:20px; background:red; . Now, I used gsap in this to increase the height of the div, which is triggered by clicked event, to 100px. But the problem i've faced is that when I try to animate only one div that has been clicked, both the div's seem to be animated. Now I know what the problem is here and I've even found the solution with jquery using $(this). But i want a Solution using Vanilla JS. Your help would be much appreciated. Thanks. 

My Code

const div = document.querySelectorAll('div');
const divArr = Array.prototype.slice.call(div);

const tl = gsap.timeline({ paused: true, reversed: true });
 
tl.to(divArr, {
    height: '100px',
    duration: 1
})

divArr.forEach(e => {

    e.addEventListener('click', function () {
        if (tl.reversed()) {

            tl.play();
        } else {
            tl.reverse(this);
        }
    })
})

 

Link to post
Share on other sites

Man, I've gotta stop browsing the forums, when you are around @PointC 😅

You always beat me by seconds.

 

 

Created an approach of my own, also suggesting the use of utils.toArray

 

See the Pen KKzmVwB by akapowl (@akapowl) on CodePen

 

 

  • Like 4
  • Haha 1
Link to post
Share on other sites

Haha. I honed my skills back in the day against Carl, Jonathan and Blake. I lost 99% of those answer battles, but I got a little faster each time I answered. :D

  • Like 2
  • Haha 1
Link to post
Share on other sites
11 hours ago, akapowl said:

Man, I've gotta stop browsing the forums, when you are around @PointC 😅

You always beat me by seconds.

 

 

Created an approach of my own, also suggesting the use of utils.toArray

 

 

 

 

 

Thank you so much for the help

Link to post
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.

×