Jump to content
GreenSock

samyH

use target from eventListener

Moderator Tag
Go to solution Solved by GreenSock,

Recommended Posts

Hey,

 

Hopefully a simple one for someone who's been using gsap more than a day :P

I'm just trying to cut down on the number of gsap.to animations I'm calling, but I'm unable to get any animation happening when passing evt.currentTarget as it's always null - I also tried evt.target & event.target.id to no avail

 

document.getElementById("menuitem0").addEventListener("mouseenter", hoverIn);

function hoverIn(evt) {
	console.log(evt);
	gsap.to(evt.currentTarget, {
		scale: 1.5,
		color: "white",
		duration: 0.3,
		paused: true,
		ease: "power2.inOut"
	});
}

 

I saw you can do something like this 

let hover1 = gsap.to(menuitem1, {
			scale: 1.5,
			color: "white",
			duration: 0.3,
			paused: true,
			ease: "power2.inOut"
		});

		document.getElementById("menuitem1").addEventListener("mouseenter", () => hover1.play());
		document.getElementById("menuitem1").addEventListener("mouseleave", () => hover1.reverse());

but need to removeEventListener, so needed to break the function out

 

Any help is much appreciated :D

Link to comment
Share on other sites

It's pretty tough to troubleshoot without a minimal demo - the issue could be caused by CSS, markup, a third party library, your browser, an external script that's totally unrelated to GSAP, etc. Would you please provide a very simple CodePen or CodeSandbox that demonstrates the issue? 

 

Please don't include your whole project. Just some colored <div> elements and the GSAP code is best (avoid frameworks if possible). See if you can recreate the issue with as few dependancies as possible. If not, incrementally add code bit by bit until it breaks. Usually people solve their own issues during this process! If not, then at least we have a reduced test case which greatly increases your chances of getting a relevant answer.

 

Here's a starter CodePen that loads all the plugins. Just click "fork" at the bottom right and make your minimal demo

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

 

If you're using something like React/Next/Nuxt/Gatsby or some other framework, you may find CodeSandbox easier to use. 

 

Once we see an isolated demo, we'll do our best to jump in and help with your GSAP-specific questions. 

Link to comment
Share on other sites

That's because you set paused: true in your tween :) So it wasn't running. 

 

I'm not sure exactly what your requirements are, but I think you could simplify things quite a bit like this: 

See the Pen QWxEzrY?editors=0110 by GreenSock (@GreenSock) on CodePen

Link to comment
Share on other sites

Ah ok that's nice, but how would I remove the hover eventListeners on click?

 

item.addEventListener("mouseenter", () => hover.play());
item.addEventListener("mouseleave", () => hover.reverse());

I thought the hover would have to be it's own function to be able to do that right?

Link to comment
Share on other sites

  • Solution
7 hours ago, samyH said:

Ah ok that's nice, but how would I remove the hover eventListeners on click?

 

There are many ways to do that. Here's one: 

See the Pen OJERNKP?editors=0010 by GreenSock (@GreenSock) on CodePen

Link to comment
Share on other sites

Ahhh ok - bloody obvious as always - thanks for helping me out - much appreciated 👍

  • Like 1
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.
×