js动画
<meta charset="utf-8">
<style>
#div1 {
width: 200px;
height: 200px;
position: relative;
background: yellow;
}
#div2 {
width: 50px;
height: 50px;
position: absolute;
background-color: red;
}
</style>
<button onclick="myMove()">点我</button>
<div id ="div1">
<div id ="div2">
</div>
</div>
<script>
function myMove() {
var pos = 0;
var clock2 = setInterval(function(){
if (150 == pos) {
clearInterval(clock2);
}
else {
pos++;
div2.style.top = pos + 'px';
div2.style.left = pos + 'px';
}
}, 20);
}
</script>
运行结果
定义和用法
语法
参数值
返回值