javascript - 单击时旋转 Font Awesome 图标

标签 javascript jquery html css

我正在尝试让 Font Awesome 人字形在点击时旋转 180º。

这是 JSFiddle 的 fiddle 到目前为止我已经尝试过了。我还希望它围绕中心旋转,所以我使用了另一个 thread .

HTML

<div class="fa fa-chevron-up"><a href="#">^</a></div>

CSS

.rotate {
-webkit-animation: spin1 2s  linear;
-moz-animation: spin1 2s  linear;
-o-animation: spin1 2s  linear;
-ms-animation: spin1 2s  linear;
animation: spin1 2s  linear;

-webkit-transform-origin: 50% 50%;
-moz-transform-origin: 50% 50%;
-o-transform-origin: 50% 50%;
transform-origin: 50% 50%;
width: 256px;
height: 256px;
}

@-webkit-keyframes spin1 {
0% { -webkit-transform: rotate(0deg);}
100% { -webkit-transform: rotate(180deg);}
}
@-moz-keyframes spin1 {
0% { -moz-transform: rotate(0deg); }
100% { -moz-transform: rotate(180deg);}
}
@-o-keyframes spin1 {
0% { -o-transform: rotate(0deg);}
100% { -o-transform: rotate(180deg);}
}
@-ms-keyframes spin1 {
0% { -ms-transform: rotate(0deg);}
100% { -ms-transform: rotate(180deg);}
}
@-keyframes spin1 {
0% { transform: rotate(0deg); }
100% { transform: rotate(180deg);}
} 

JS

$(".fa-chevron-up").click(function(){
 $(this).toggleClass("rotate")  ; 
})

最佳答案

我相信使用 CSS 过渡会更容易做到这一点:

CSS

.rotate{
    -moz-transition: all 2s linear;
    -webkit-transition: all 2s linear;
    transition: all 2s linear;
}

.rotate.down{
    -ms-transform: rotate(180deg);
    -moz-transform: rotate(180deg);
    -webkit-transform: rotate(180deg);
    transform: rotate(180deg);
}

jQuery

$(".rotate").click(function(){
    $(this).toggleClass("down"); 
});

Demo fiddle

关于javascript - 单击时旋转 Font Awesome 图标,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26173376/

相关文章:

javascript - 对于经验丰富的非网络程序员来说,网络编程应该从哪些书籍开始?

jquery - 当元素从 dom 中移除时移除 qtip

javascript - 为什么 jQuery.parseJSON 在 Firefox 中无法解析这个有效的 JSON 文档?

html - 动画在 chrome 中不起作用

javascript - chop/增加 div 中的字体大小以使其适合

javascript - 源标记和绝对 URL

javascript - 使用 Ajax 检查跨域 RSS 是否已更改

javascript - 填充输入字段时重新启用禁用的命令按钮

java - Java 桌面 GUI 内的 HTML5 View 、JavaScript 通信

javascript - 用于检测视频点击的跨浏览器解决方案?