-
Notifications
You must be signed in to change notification settings - Fork 0
/
animation.html
52 lines (47 loc) · 981 Bytes
/
animation.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
<!DOCTYPE html>
<html>
<style>
#container {
width: 400px;
height: 400px;
position: relative;
background: gray;
}
#sprite {
width: 50px;
height: 50px;
position: absolute;
background-color: blue;
}
</style>
<body>
<p><button id="button" onclick="animate()">Animate</button></p>
<div id ="container">
<div id ="sprite"></div>
</div>
<script>
animate = (function() {
var pos=0, inc=1;
return function() {
var elem = document.getElementById("sprite");
var button = document.getElementById("button");
var id = setInterval(frame, 5);
button.innerHTML = "Stop";
button.onclick = function() {
clearInterval(id);
button.innerHTML = "Animate";
button.onclick = function() { animate(); };
};
function frame() {
if ((pos >= 350) || (pos < 0)) {
inc = -inc;
}
pos += inc;
elem.style.top = pos + "px";
elem.style.left = pos + "px";
};
};
})();
</script>
</body>
</html>