Jump to content
Search Community

Animating with a Click: Code not Working

amanfromsolan test
Moderator Tag

Go to solution Solved by joe_midi,

Warning: Please note

This thread was started before GSAP 3 was released. Some information, especially the syntax, may be out of date for GSAP 3. Please see the GSAP 3 migration guide and release notes for more information about how to update the code to GSAP 3's syntax. 

Recommended Posts

Hello there!

I'm just starting out on JavaScript, and I found out GSAP animations are 20X faster than jQuery's (thanks to this article over here: https://css-tricks.com/myth-busting-css-animations-vs-javascript/), so I'm keen on learning it.

 

I have a navigation bar that should close as soon as I press the close button. But it closes as soon as the page loads (not on the click). I'd appreciate your help. :)

See the Pen VKPmZP by amanfromsolan (@amanfromsolan) on CodePen

Link to comment
Share on other sites

  • Solution

Hey @amanfromsolan!

 

I noticed two things about your code:

var button = document.getElementsByTagName("button");

button.onclick = navAnimation.play();

getElementsByTagName will actually return an array of all the elements with that tag name, so you'll need to place the click onto its index within the array, like button[0].

 

 

And you need to assign on click a function, rather than associating navAnimation.play() to it.

 

So more like:

button[0].onclick = function() {
 navAnimation.play();
}

See the Pen vXggyV by joemidi (@joemidi) on CodePen

  • Like 3
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...