Continuous CSS rotation animation on hover, animated back to 0deg on hover out

后端 未结 7 1543
温柔的废话
温柔的废话 2020-11-30 22:26

I have an element that spins when you hover over it indefinitely. When you hover out, the animation stops. Simple:

@-webkit-keyframes rotate {
    from {
            


        
7条回答
  •  小蘑菇
    小蘑菇 (楼主)
    2020-11-30 22:57

    Here's a javascript implementation that works with web-kit:

    var isHovering = false;
    
    var el = $(".elem").mouseover(function(){
        isHovering = true;
        spin();
    }).mouseout(function(){
        isHovering = false;
    });
    
    var spin = function(){
        if(isHovering){
            el.removeClass("spin");
    
            setTimeout(function(){
                el.addClass("spin");
    
                setTimeout(spin, 1500);
            }, 0);
        }
    };
    spin();
    

    JSFiddle: http://jsfiddle.net/4Vz63/161/

    Barf.

提交回复
热议问题