当我单击两次时,css3 转换不起作用
when I click twice css3 transform is not working
我想要单击开始按钮时的方形旋转。但是:
当我单击一次时,它正在工作。 当我运行两次时,此 css3 效果不起作用。
$(function(){
var $obj=$(".red");
$("#btnStart").on("click",function(){
$obj.removeClass("run").addClass("run");
});
});
body{padding:100px;}
.red{background:#f00;width:100px;height: 100px;}
.run{
transform:rotate(3600deg);
transition: transform 5s ease 0s;
-moz-transition:transform 5s ease 0s;
-webkit-transition:transform 5s ease 0s;
-o-transition:transform 5s ease 0s;
}
<html>
<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
</head>
<body>
<div class="red"></div>
<br/>
<button id="btnStart">
开始旋转
</button>
</body>
</html>
在删除和添加类之间需要有一个超时。 否则,它将无法识别更改。
$(function(){
var $obj=$(".red");
$("#btnStart").on("click",function(){
$obj.removeClass("run");
setTimeout(function(){
$obj.addClass("run");
}, 10);
});
});
body{padding:100px;}
.red{background:#f00;width:100px;height: 100px;}
.run{
transform:rotate(3600deg);
transition: transform 5s ease 0s;
-moz-transition:transform 5s ease 0s;
-webkit-transition:transform 5s ease 0s;
-o-transition:transform 5s ease 0s;
}
<html>
<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
</head>
<body>
<div class="red"></div><br/>
<button id="btnStart">
开始旋转
</button>
</body>
</html>
你可以使用 jQuery 的函数animate()
而不是添加/删除类!
$(function(){
var $obj=$(".red");
$("#btnStart").click(function(){
$('.red').animate({ borderSpacing: -3600 }, {
step: function(now,fx) {
$(this).css('-webkit-transform','rotate('+now+'deg)');
$(this).css('-moz-transform','rotate('+now+'deg)');
$(this).css('transform','rotate('+now+'deg)');
},
duration:'slow'
},'linear');
});
});
body{padding:100px;}
.red{background:#f00;width:100px;height: 100px;}
<html>
<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
</head>
<body>
<div class="red"></div>
<br/>
<button id="btnStart">
开始旋转
</button>
</body>
</html>
相关文章:
- css3转换和javascript出错
- CSS3转换导致非常奇怪的window.scrollY行为
- CSS3转换和javascript交互
- 如何使用CSS3转换
- 更改CSS3转换值
- 添加CSS3转换浮动导航栏-JQUERY
- 添加CSS3转换展开/折叠
- 滚动条不可见,CSS3转换在MOZ中不起作用
- CSS3转换+jQuery翻转卡问题
- 类似css3转换的Javascript
- 如何在 jquery transit 中暂停和重新启动 css3 转换
- 使用 Javascript 的 CSS3 转换
- 为什么这个 CSS3 转换没有被触发
- CSS3 转换 - 如何延迟 z 索引属性的重置
- 在*最后一个*CSS3转换之后,事件处理的稳健方式
- 如何在IE中访问css3转换旋转
- 是否可以在Javascript中的css3转换期间获得目标css属性值
- 如何使用属性显示带有CSS3转换的隐藏元素
- 用户查看页面后启动CSS3转换
- 使用点击jquery事件执行css3转换