cbourne Posted March 8, 2023 Share Posted March 8, 2023 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 More sharing options...
Toso Posted March 8, 2023 Share Posted March 8, 2023 I changed {style: "fill: #F2653A", duration: 1} to {background:" #F2653A", duration: 1} and it worked just fine Link to comment Share on other sites More sharing options...
cbourne Posted March 8, 2023 Author Share Posted March 8, 2023 OK so I made some progress and update the pen. However, I'm getting... "Invalid property" "style" "set to" "fill:#1ABFD3" "Missing plugin? gsap.registerPlugin()" .. when the event fires. Link to comment Share on other sites More sharing options...
Solution Toso Posted March 8, 2023 Solution Share Posted March 8, 2023 please try this one and let me know if that's what you looking for See the Pen MWqEdYN?editors=0110 by ahmed-attia (@ahmed-attia) on CodePen 3 Link to comment Share on other sites More sharing options...
cbourne Posted March 8, 2023 Author Share Posted March 8, 2023 Perfect that did the trick. Thanks very much. Only thing I needed to do was change the `background` to `fill` for it to work with my SVG. Link to comment Share on other sites More sharing options...
GreenSock Posted March 8, 2023 Share Posted March 8, 2023 Nice job, @Toso ? Minor tweaks: See the Pen KKxXjvW?editors=0010 by GreenSock (@GreenSock) on CodePen I'd use backgroundColor rather than background, and I'd set the overwrite to either true or "auto" just in case the user rolls over/out quickly, you don't want to create conflicting animations. 2 Link to comment Share on other sites More sharing options...
cbourne Posted March 8, 2023 Author Share Posted March 8, 2023 Don't think "backgroundColor" works with SVG. Is there a universal attribute ? Link to comment Share on other sites More sharing options...
Rodrigo Posted March 8, 2023 Share Posted March 8, 2023 Hi, For SVG is best to use the Attribute Plugin: https://greensock.com/docs/v3/GSAP/CorePlugins/AttrPlugin So in this case it would be: gsap.to(['#box1', '#box2'], { attr: { fill: "#F2653A" }, duration: 1 }); Where box1 and box2 are SVG elements. Hopefully this helps. Happy Tweening! Link to comment Share on other sites More sharing options...
cbourne Posted March 8, 2023 Author Share Posted March 8, 2023 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 More sharing options...
cbourne Posted March 8, 2023 Author Share Posted March 8, 2023 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 More sharing options...
Rodrigo Posted March 8, 2023 Share Posted March 8, 2023 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 More sharing options...
cbourne Posted March 8, 2023 Author Share Posted March 8, 2023 Here's what I now have: See the Pen mdGBNmO by carlskii (@carlskii) on CodePen Link to comment Share on other sites More sharing options...
Rodrigo Posted March 8, 2023 Share Posted March 8, 2023 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! 1 Link to comment Share on other sites More sharing options...
PointC Posted March 8, 2023 Share Posted March 8, 2023 Exact same thing with SVG fill and the attrPlugin. Happy tweening. See the Pen 19b1b9a98a4cd015fcdfebb25cb2d513 by PointC (@PointC) on CodePen 2 Link to comment Share on other sites More sharing options...
cbourne Posted March 9, 2023 Author Share Posted March 9, 2023 Perfect this is exactly what I needed. This works with my SVG nicely. 1 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