为什么不直接点击使用toggleClass?
JS:
$(this).toggleClass("up");
CSS:
button.up {
-webkit-transform: rotate(180deg);
-moz-transform: rotate(180deg);
-ms-transform: rotate(180deg);
-o-transform: rotate(180deg);
transform: rotate(180deg);
/* IE6–IE9 */
filter: progid:DXImageTransform.Microsoft.Matrix(M11=0.9914448613738104, M12=-0.13052619222005157,M21=0.13052619222005157, M22=0.9914448613738104, sizingMethod='auto expand');
zoom: 1;
}
也可以将它添加到CSS:
button{
-webkit-transition: all 500ms ease-in-out;
-moz-transition: all 500ms ease-in-out;
-o-transition: all 500ms ease-in-out;
-ms-transition: all 500ms ease-in-out;
}
这将增加动画。
PS ...
回答你原来的问题:
你说,它旋转,但从来没有停止。当使用set timeout时,你需要确保你有一个不会调用settimeout的条件,否则它将永远运行。因此,对于您的代码:
<script type="text/javascript">
$(function() {
var $elie = $("#bkgimg");
rotate(0);
function rotate(degree) {
// For webkit browsers: e.g. Chrome
$elie.css({ WebkitTransform: 'rotate(' + degree + 'deg)'});
// For Mozilla browser: e.g. Firefox
$elie.css({ '-moz-transform': 'rotate(' + degree + 'deg)'});
/* add a condition here for the extremity */
if(degree < 180){
// Animate rotation with a recursive call
setTimeout(function() { rotate(++degree); },65);
}
}
});
</script>
来源
2012-02-24 23:00:52
cpi
这只是旋转图像。我真的希望它动画/旋转,并停在某个点或某个程度。对不起,我应该在之前提到过。谢谢。 – Andrea
@Andrea,我在Safari中查看你的页面,图像旋转45度。在控制台中查看DOM,它显示已经应用了'-webkit-transform'的正确CSS。也许你的浏览器不支持CSS 3. – Sparky
@Andrea,只需将'.css'改为'.animate'即可。看我的编辑。 – Sparky