1<!DOCTYPE html>
2<html>
3<style>
4#myContainer {
5 width: 400px;
6 height: 400px;
7 position: relative;
8 background: yellow;
9}
10#myAnimation {
11 width: 50px;
12 height: 50px;
13 position: absolute;
14 background-color: red;
15}
16</style>
17<body>
18<p>
19<button onclick="myMove()">Click Me</button>
20</p>
21<div id ="myContainer">
22<div id ="myAnimation"></div>
23</div>
24<script>
25var id = null;
26function myMove() {
27 var elem = document.getElementById("myAnimation");
28 var pos = 0;
29 clearInterval(id);
30 id = setInterval(frame, 10);
31 function frame() {
32 if (pos == 350) {
33 clearInterval(id);
34 } else {
35 pos++;
36 elem.style.top = pos + 'px';
37 elem.style.left = pos + 'px';
38 }
39 }
40}
41</script>
42</body>
43</html>