Reputation: 33
I'm trying to let a cricle spin and on mouseenter I want the circle to stop spinning and rotate "x" degree, on mouseleave the circle should spin back to its origin and continue the spinning animation.
function rotatingCircle(degree) {
var test =
anime({
targets: [document.getElementsByTagName("svg")[0]],
rotate: degree,
duration: 800,
loop: true,
elasticity: 0,
easing: "linear"
});
};
rotatingCircle(360);
var buttonEl = document.querySelector('.trigger');
var circleOne = document.getElementsByTagName("svg");
buttonEl.addEventListener('mouseenter', rotatingCircle(20));
buttonEl.addEventListener('mouseleave', rotatingCircle(0));
Add the moment my svg is not rotating at all. When I remove the 'mouseenter' and 'mouseleave' event, then my svg is rotating as wanted. How do I keep a rotating loop animation AND can add a 'mouseenter' / 'mouseleave' event, which triggers the rotating SVG to rotate to a specifig angle?
Upvotes: 1
Views: 1852
Reputation: 1276
buttonEl.addEventListener('mouseenter', rotatingCircle(20));
buttonEl.addEventListener('mouseleave', rotatingCircle(0));
are actually calling the function rotatingCircle
where as you want to send a function name or anonymous function here instead.
buttonEl.addEventListener('mouseenter', rotatingCircle);
buttonEl.addEventListener('mouseleave', rotatingCircle);
buttonEl.addEventListener('mouseenter', () => rotatingCircle(20));
buttonEl.addEventListener('mouseleave', () => rotatingCircle(20));
listeners just need the reference to what to call when they are triggered, they don't need what the function returns from calling
I made a code sandbox of what I think you are talking about. Check it out here: https://codesandbox.io/s/aninejs-different-animations-for-different-hover-states-ld76r
Upvotes: 1