console
function myMove() {
var elem = document.getElementById("myAnimation");
var pos = 0;
var id = setInterval(frame, 10);
function frame() {
if (pos == 500) {
clearInterval(id);
} else {
pos++;
elem.style.top = pos + 'px';
elem.style.left = pos + 'px';
}
}
}
function myMove() {
var elem = document.getElementById("myAnimation");
var pos = 0;
var id = setInterval(frame, 1);
function frame() {
if (pos == 350) {
clearInterval(id);
} else {
pos++;
elem.style.top = pos + 'px';
elem.style.left = pos + 'px';
}
}
}
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>JavaScript 动画应用实例</title>
<link rel="stylesheet" type="text/css" href="a.css" />
<script type="text/javascript" src="a.js"></script>
</head>
<body>
<p>
<button onclick="myMove()">点我</button>
</p>
<div id ="myContainer">
<div id ="myAnimation"></div>
</div>
</body>
</html>
#myContainer {
width: 400px;
height: 400px;
position: relative;
background: rgb(236, 140, 13);
}
#myAnimation {
width: 50px;
height: 50px;
position: absolute;
background-color: rgba(144, 22, 224, 0.918);
}