Jump to content
Search Community

Binding EventListeners to multiple ID's

cbourne test
Moderator Tag

Go to solution Solved by Toso,

Recommended Posts

Hi,

I have page with some some squares defined as buttons. I have a function that attaches an onClick handler to each the buttons which works fine, however I can't work out how to add a mouseover animation to each of the buttons. 

I've attached a codepen that demonstrates what I have so far. 

See the Pen WNgZWeZ by carlskii (@carlskii) on CodePen

Link to comment
Share on other sites

Hmm.. So I tried the Attribute Plugin using this code, but it doesn't work with my SVG. 

let elements = document.querySelectorAll('.button');

    let clickEvent = (e) => {
      console.log('some event content here...')
      console.log("----------------" + e.target.id + "----------------------")
    }
    elements.forEach((item) => {
      item.addEventListener("mouseenter", ()=>{
		gsap.to(item, {attr: {fill: "#F2653A"}, duration: 1, overwrite: "auto" });
      });
      item.addEventListener("mouseout", ()=>{
            gsap.to(item, {attr: {fill: "red"} ,duration: 1, overwrite: "auto" });
      }); 
      item.addEventListener('click', clickEvent)

      });

	});

 

Link to comment
Share on other sites

Hmm.. So I tried the Attribute Plugin using this code, but it doesn't work with my SVG. Actually it doesn't seem to work with the basic div's either.

let elements = document.querySelectorAll('.button');

    let clickEvent = (e) => {
      console.log('some event content here...')
      console.log("----------------" + e.target.id + "----------------------")
    }
    elements.forEach((item) => {
      item.addEventListener("mouseenter", ()=>{
		gsap.to(item, {attr: {fill: "#F2653A"}, duration: 1, overwrite: "auto" });
      });
      item.addEventListener("mouseout", ()=>{
            gsap.to(item, {attr: {fill: "red"} ,duration: 1, overwrite: "auto" });
      }); 
      item.addEventListener('click', clickEvent)

      });

	});

 

Link to comment
Share on other sites

Hi,

 

Fill is not a CSS property of regular DOM elements and, as stated in the documentation, the Attribute Plugin is not meant for DOM elements when you animate CSS properties, that's what the CSS Plugin is for.

 

Please create a minimal demo that shows exactly what you're trying to do.

 

Happy Tweening!

Link to comment
Share on other sites

Hi,

 

There are no SVG elements in your code so this I can't understand correctly:

34 minutes ago, cbourne said:

So I tried the Attribute Plugin using this code, but it doesn't work with my SVG

The solutions provided by @Toso and Jack should be enough for the codepen example you have. As mentioned before there is no need for the Attribute Plugin if you want to animate the background color of a DIV or any other regular DOM element, just follow the code that has already been provided and you should be fine. Unless your real life scenario does include SVG elements, but you're not including them in the codepen examples you're providing, so we're running in circles here.

 

Happy Tweening!

  • 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.
×
×
  • Create New...