css3 变换图像的旋转

标签 css animation rotation

我想在点击图像时将其旋转 180 度。然后在随后的点击中将同一图像再旋转 180 度(完成旋转)。

我已经实现了第一部分:

.img_rotator_180 { 
-webkit-transform: rotate(180deg); 
-moz-transform: rotate(180deg); 
-o-transform: rotate(180deg);
-ms-transform: rotate(180deg); 
}
.img_rotator_360 { 
-webkit-transform: rotate(180deg); 
-moz-transform: rotate(180deg); 
-o-transform: rotate(180deg);
-ms-transform: rotate(180deg); 
}
.img_rotator_transition {
  -webkit-transition: all 1s ease-out;  /* Chrome 1-25, Safari 3.2+ */
     -moz-transition: all 1s ease-out;  /* Firefox 4-15 */
       -o-transition: all 1s ease-out;  /* Opera 10.50–12.00 */
          transition: all 1s ease-out;  /* Chrome 26, Firefox 16+, IE 10+, Opera 12.50+
}

$('div.' + this.id + ' img').addClass('img_rotator_180 img_rotator_transition');

替代版本:

$('div.' + this.id + ' img').addClass('img_rotator_180 img_rotator_transition');

这样做的结果是图像的初始旋转不会被记住,这意味着第二次旋转实际上是重新进行了 180 度旋转。

有没有办法在应用进一步变换之前建立图像当前旋转?或者也许是一种附加旋转而不是替换它的方法?

谢谢

最佳答案

Demo Fiddle

HTML

<div>Click Me!</div>

jQuery

$('div').on('click', function () {
    if ($(this).hasClass('spinIn')) {
        $(this).addClass('spinOut').removeClass('spinIn');
    } else {
        $(this).addClass('spinIn').removeClass('spinOut');
    }
});

CSS

div {
    display:inline-block;
}
.spinIn {
    -webkit-animation: spinIn 0.6s 1 linear;
    animation: spinIn 0.6s 1 linear;
    -webkit-animation-fill-mode:forwards;
    animation-fill-mode:forwards;
}
@-webkit-keyframes spinIn {
    0% {
        -webkit-transform: rotate(0deg);
    }
    100% {
        -webkit-transform: rotate(180deg);
    }
}
@keyframes spinIn {
    0% {
        transform: rotate(0deg);
    }
    100% {
        transform: rotate(180deg);
    }
}
.spinOut {
    -webkit-animation: spinOut 0.6s 1 linear;
    animation: spinOut 0.6s 1 linear;
    -webkit-animation-fill-mode:forwards;
    animation-fill-mode:forwards;
}
@-webkit-keyframes spinOut {
    0% {
        -webkit-transform: rotate(180deg);
    }
    100% {
        -webkit-transform: rotate(360deg);
    }
}
@keyframes spinOut {
    0% {
        transform: rotate(180deg);
    }
    100% {
        transform: rotate(360deg);
    }
}

关于css3 变换图像的旋转,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23492404/

相关文章:

css - Wordpress - 在所有页面上的服务器 404 上上传后,没有 css

javascript - js脚本在段落中插入文字时logo移动

python - (python) matplotlib 动画不显示

css - CSS3中自动调整图像效果

xaml - 在 UWP 中设置 GradientBrush 动画

iphone - 设置自动旋转框架后,UIButtons 不响应触摸

CSS边框半径第二半径说明

css - 具有 div 显示属性的媒体查询

Android:将imageview中的图像旋转一个角度

android - 带有屏幕旋转的 Fragment 上的 DialogFragment