前端复习--js控制CSS3动画

本文介绍如何使用CSS和JavaScript实现一个简单的按钮控制DOM元素旋转动画,并加入transition平滑过渡效果。

废话不说,先来个简单的:利用按钮控制旋转(通过控制DOM元素的className属性实现)

<!DOCTYPE html>
<html>
<head>
<style> 
.normal{
margin:30px;
width:200px;
height:100px;
background-color:yellow;
}
.rotate{
margin:30px;
width:200px;
height:100px;
background-color:yellow;
-webkit-transform:rotate(9deg); 
}
</style>
</head>
<body>

<button id="roll">rotate</button>
<div id="td" class="normal"></div>


<script type="text/javascript">
var btn = document.getElementById("roll");
var box = document.getElementById("td");
btn.onclick = function(){
   if(box.className == "normal"){
      box.className = "rotate";
   }else{
      box.className = "normal";
   }
}
</script>
</body>
</html>

复杂一点,加入transition效果

<!DOCTYPE html>
<html>
<head>
<style> 
.normal{
margin:30px;
width:200px;
height:100px;
background-color:yellow;
}
.rotate{
margin:30px;
width:200px;
height:100px;
background-color:yellow;
-webkit-transition: -webkit-transform 2s;
-webkit-transform:rotate(90deg); 
}
</style>
</head>
<body>

<button id="roll">rotate</button>
<div id="td" class="normal"></div>


<script type="text/javascript">
var btn = document.getElementById("roll");
var box = document.getElementById("td");
btn.onclick = function(){
   if(box.className == "normal"){
      box.className = "rotate";
   }else{
      box.className = "normal";
   }
}
</script>
</body>
</html>


了解 window.getComputedStyle(node)

评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值