I'm trying to make a few elements on my website rotate loopingly on hover. I've done it with css keyframe animation but when I mouse over or out, the element jumps uglingly.
So I'm trying to do it with some fancy jQuery (since I already have it loaded on my page) and I came across this very helpful stack which the answer explains how to loop with the hover. BUT the explanation is for movement. I managed to move it around with hover, but would be cool to rotate (maybe even both, if it can handle it).
Code for looping:
$.fn.loopingAnimation = function(props, dur, eas) {
if (this.data('loop') === true)
{
this.animate( props, dur, eas, function() {
if( $(this).data('loop') === true ) $(this).loopingAnimation(props, dur, eas);
});
}
return this; // Don't break the chain
};
$("#animate").hover(function(){
$(this).data('loop', true).stop().loopingAnimation({ 'margin-top': '50px'}, 500).loopingAnimation({'margin-top':'10px'},500);
}, function(){
$(this).data('loop', false).stop();
// Now our animation will stop after fully completing its last cycle
});
I then came across yet another stack which explains how to animate rotation with jQuery, but now I can't seem to be able to meld both codes together...
code for rotate:
$(document).ready(function (){
$({deg: 0}).animate({deg: 90}, {
duration: 2000,
step: function(now){
$("#rotate").css({
transform: "rotate(" + now + "deg)"
});
}
});
}
);
I made a pen with both codes to get things easier. Anyone knows how to do it?