悬停时连续的CSS旋转动画,在悬停时动画回0deg

时间:2022-11-05 20:05:04

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

当你无限期地悬停它时,我有一个旋转的元素。当您将鼠标悬停时,动画将停止。简单:

@-webkit-keyframes rotate {
    from {
        -webkit-transform: rotate(0deg);
    }
    to { 
        -webkit-transform: rotate(360deg);
    }
}

.elem:hover {
    -webkit-animation-name: rotate; 
    -webkit-animation-duration: 1.5s; 
    -webkit-animation-iteration-count: infinite;
    -webkit-animation-timing-function: linear;
}

When you hover out, though, the animation abruptly ceases, reverting to 0 degrees. I'd like to animate back to that position, but I'm having some trouble working out the syntax.

但是当你徘徊时,动画会突然停止,恢复到0度。我想动画回到那个位置,但是我在编写语法时遇到了一些麻烦。

Any input would be awesome!

任何输入都会很棒!

7 个解决方案

#1


74  

The solution is to set the default value in your .elem. But this annimation work fine with -moz but not yet implement in -webkit

解决方案是在.elem中设置默认值。但是这个annimation与-moz一起工作正常,但还没有在-webkit中实现

Look at the fiddle I updated from yours : http://jsfiddle.net/DoubleYo/4Vz63/1648/

看看我从你那里更新的小提琴:http://jsfiddle.net/DoubleYo/4Vz63/1648/

It works fine with Firefox but not with Chrome

它适用于Firefox,但不适用于Chrome

.elem{
    position: absolute;
    top: 40px;
    left: 40px;
    width: 0; 
    height: 0;
    border-style: solid;
    border-width: 75px;
    border-color: red blue green orange;
    transition-property: transform;
    transition-duration: 1s;
}
.elem:hover {
    animation-name: rotate; 
    animation-duration: 2s; 
    animation-iteration-count: infinite;
    animation-timing-function: linear;
}

@keyframes rotate {
    from {transform: rotate(0deg);}
    to {transform: rotate(360deg);}
}
<div class="elem"></div>

#2


14  

Here's a simple working solution:

这是一个简单的工作解决方案:

@-moz-keyframes spin { 100% { -moz-transform: rotate(360deg); } }
@-webkit-keyframes spin { 100% { -webkit-transform: rotate(360deg); } }
@keyframes spin { 100% { -webkit-transform: rotate(360deg); transform:rotate(360deg); } }

.elem:hover {
    -webkit-animation:spin 1.5s linear infinite;
    -moz-animation:spin 1.5s linear infinite;
    animation:spin 1.5s linear infinite;
}

#3


1  

It took a few tries, but I was able to get your jsFiddle to work (for Webkit only).

它花了几次尝试,但我能够让你的jsFiddle工作(仅适用于Webkit)。

There's still an issue with the animation speed when the user re-enters the div.

当用户重新进入div时,动画速度仍然存在问题。

Basically, just set the current rotation value to a variable, then do some calculations on that value (to convert to degrees), then set that value back to the element on mouse move and mouse enter.

基本上,只需将当前旋转值设置为变量,然后对该值进行一些计算(转换为度数),然后在鼠标移动和鼠标输入时将该值设置回元素。

Check out the jsFiddle: http://jsfiddle.net/4Vz63/46/

看看jsFiddle:http://jsfiddle.net/4Vz63/46/

Check out this article for more information, including how to add cross-browser compatibility: http://css-tricks.com/get-value-of-css-rotation-through-javascript/

查看此文章以获取更多信息,包括如何添加跨浏览器兼容性:http://css-tricks.com/get-value-of-css-rotation-through-javascript/

#4


1  

Cross browser compatible JS solution:

跨浏览器兼容JS解决方案:

var e = document.getElementById('elem');
var spin = false;

var spinner = function(){
e.classList.toggle('running', spin);
if (spin) setTimeout(spinner, 2000);
}

e.onmouseover = function(){
spin = true;
spinner();
};

e.onmouseout = function(){
spin = false;
};
body { 
height:300px; 
}
#elem {
position:absolute;
top:20%;
left:20%;
width:0; 
height:0;
border-style: solid;
border-width: 75px;
border-color: red blue green orange;
border-radius: 75px;
}

#elem.running {
animation: spin 2s linear 0s infinite;
}

@keyframes spin { 
100% { transform: rotate(360deg); } 
}
<div id="elem"></div>

#5


0  

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

这是一个适用于web-kit的javascript实现:

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/

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

Barf.

BARF。

#6


-1  

You should trigger the animation to revert once it's completed w/ javascript.

一旦完成w / javascript,你应该触发动画恢复。

  $(".item").live("animationend webkitAnimationEnd", function(){
    $(this).removeClass('animate');
  });

#7


-1  

<script>
var deg = 0

function rotate(id)
{
    deg = deg+45;
    var txt = 'rotate('+deg+'deg)';
    $('#'+id).css('-webkit-transform',txt);
}
</script>

What I do is something very easy... declare a global variable at the start... and then increment the variable however much I like, and use .css of jquery to increment.

我所做的事情非常简单......在开始时声明一个全局变量...然后按照我喜欢的方式递增变量,并使用jquery的.css来递增。

#1


74  

The solution is to set the default value in your .elem. But this annimation work fine with -moz but not yet implement in -webkit

解决方案是在.elem中设置默认值。但是这个annimation与-moz一起工作正常,但还没有在-webkit中实现

Look at the fiddle I updated from yours : http://jsfiddle.net/DoubleYo/4Vz63/1648/

看看我从你那里更新的小提琴:http://jsfiddle.net/DoubleYo/4Vz63/1648/

It works fine with Firefox but not with Chrome

它适用于Firefox,但不适用于Chrome

.elem{
    position: absolute;
    top: 40px;
    left: 40px;
    width: 0; 
    height: 0;
    border-style: solid;
    border-width: 75px;
    border-color: red blue green orange;
    transition-property: transform;
    transition-duration: 1s;
}
.elem:hover {
    animation-name: rotate; 
    animation-duration: 2s; 
    animation-iteration-count: infinite;
    animation-timing-function: linear;
}

@keyframes rotate {
    from {transform: rotate(0deg);}
    to {transform: rotate(360deg);}
}
<div class="elem"></div>

#2


14  

Here's a simple working solution:

这是一个简单的工作解决方案:

@-moz-keyframes spin { 100% { -moz-transform: rotate(360deg); } }
@-webkit-keyframes spin { 100% { -webkit-transform: rotate(360deg); } }
@keyframes spin { 100% { -webkit-transform: rotate(360deg); transform:rotate(360deg); } }

.elem:hover {
    -webkit-animation:spin 1.5s linear infinite;
    -moz-animation:spin 1.5s linear infinite;
    animation:spin 1.5s linear infinite;
}

#3


1  

It took a few tries, but I was able to get your jsFiddle to work (for Webkit only).

它花了几次尝试,但我能够让你的jsFiddle工作(仅适用于Webkit)。

There's still an issue with the animation speed when the user re-enters the div.

当用户重新进入div时,动画速度仍然存在问题。

Basically, just set the current rotation value to a variable, then do some calculations on that value (to convert to degrees), then set that value back to the element on mouse move and mouse enter.

基本上,只需将当前旋转值设置为变量,然后对该值进行一些计算(转换为度数),然后在鼠标移动和鼠标输入时将该值设置回元素。

Check out the jsFiddle: http://jsfiddle.net/4Vz63/46/

看看jsFiddle:http://jsfiddle.net/4Vz63/46/

Check out this article for more information, including how to add cross-browser compatibility: http://css-tricks.com/get-value-of-css-rotation-through-javascript/

查看此文章以获取更多信息,包括如何添加跨浏览器兼容性:http://css-tricks.com/get-value-of-css-rotation-through-javascript/

#4


1  

Cross browser compatible JS solution:

跨浏览器兼容JS解决方案:

var e = document.getElementById('elem');
var spin = false;

var spinner = function(){
e.classList.toggle('running', spin);
if (spin) setTimeout(spinner, 2000);
}

e.onmouseover = function(){
spin = true;
spinner();
};

e.onmouseout = function(){
spin = false;
};
body { 
height:300px; 
}
#elem {
position:absolute;
top:20%;
left:20%;
width:0; 
height:0;
border-style: solid;
border-width: 75px;
border-color: red blue green orange;
border-radius: 75px;
}

#elem.running {
animation: spin 2s linear 0s infinite;
}

@keyframes spin { 
100% { transform: rotate(360deg); } 
}
<div id="elem"></div>

#5


0  

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

这是一个适用于web-kit的javascript实现:

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/

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

Barf.

BARF。

#6


-1  

You should trigger the animation to revert once it's completed w/ javascript.

一旦完成w / javascript,你应该触发动画恢复。

  $(".item").live("animationend webkitAnimationEnd", function(){
    $(this).removeClass('animate');
  });

#7


-1  

<script>
var deg = 0

function rotate(id)
{
    deg = deg+45;
    var txt = 'rotate('+deg+'deg)';
    $('#'+id).css('-webkit-transform',txt);
}
</script>

What I do is something very easy... declare a global variable at the start... and then increment the variable however much I like, and use .css of jquery to increment.

我所做的事情非常简单......在开始时声明一个全局变量...然后按照我喜欢的方式递增变量,并使用jquery的.css来递增。