javascript动画之闪动动画: demo代码:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Document</title>
<style type="text/css">
div {
width: 100px;
height: 100px;
background: red;
margin-top: 20px;
position: relative;
}
</style>
</head>
<body>
<button id="btn">按钮</button>
<div id="box"></div>
<script type="text/javascript">
var btn = document.getElementsByTagName("button")[0];
var box = document.getElementsByTagName("div")[0];
btn.onclick = function(){
box.style.left = "500px";//记住使用style.left时,必须定位。
}
</script>
</body>
</html>
javascript动画之匀速运动动画。demo代码如下:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Document</title>
<style type="text/css">
div {
width: 100px;
height: 100px;
background: red;
margin-top: 20px;
position: absolute;
left: 0;
}
</style>
</head>
<body>
<button id="btn">按钮</button>
<div id="box"></div>
<script type="text/javascript">
var btn = document.getElementsByTagName("button")[0];
var box = document.getElementsByTagName("div")[0];
var timer = null;
btn.onclick = function(){
timer = setInterval(function(){
box.style.left = box.offsetLeft + 10 + "px";
if(box.offsetLeft >= 200){
clearInterval(timer);
}
},30);
}
</script>
</body>
</html>