-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex-javascript.html
More file actions
120 lines (112 loc) · 3.41 KB
/
Copy pathindex-javascript.html
File metadata and controls
120 lines (112 loc) · 3.41 KB
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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
<!doctype html>
<html lang="en">
<head>
<script
async
src="https://www.googletagmanager.com/gtag/js?id=G-063933E3C2"
></script>
<script>
window.dataLayer = window.dataLayer || [];
function gtag() {
dataLayer.push(arguments);
}
gtag("js", new Date());
gtag("config", "G-063933E3C2");
</script>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Bouncing ball in JavaScript</title>
<link
rel="canonical"
href="https://evoluteur.github.io/bouncing-ball/index-javascript.html"
/>
<meta name="author" content="Olivier Giulieri" />
<meta name="robots" content="index, follow" />
<meta
name="description"
content="Bouncing ball animation demo in JavaScript using setInterval and DOM positioning. Minimal example showing how to animate elements with JavaScript."
/>
<meta
name="keywords"
content="bouncing, ball, javascript, animated, animation, setInterval, DOM, transition, fullscreen, sample, code"
/>
<meta property="og:type" content="website" />
<meta
property="og:url"
content="https://evoluteur.github.io/bouncing-ball/index-javascript.html"
/>
<meta property="og:title" content="Bouncing ball in JavaScript" />
<meta
property="og:description"
content="Bouncing ball animation demo in JavaScript using setInterval and DOM positioning. Minimal example showing how to animate elements with JavaScript."
/>
<meta name="twitter:card" content="summary" />
<meta name="twitter:title" content="Bouncing ball in JavaScript" />
<meta
name="twitter:description"
content="Bouncing ball animation demo in JavaScript using setInterval and DOM positioning."
/>
<link rel="icon" type="image/png" href="favicon.png" />
<link
rel="stylesheet"
href="https://fonts.googleapis.com/css?family=Overpass"
/>
<link rel="stylesheet" href="bball.css" />
<script>
let x = 0,
y = 0,
maxX,
maxY,
upDown = 1,
leftRight = 1;
function setDim() {
maxX = window.innerWidth - 50;
if (x > maxX) {
x = maxX;
}
maxY = window.innerHeight - 50;
if (y > maxY) {
y = maxY;
}
}
function bounce() {
if (x > maxX || x < 0) {
leftRight = -leftRight;
}
if (y > maxY || y < 0) {
upDown = -upDown;
}
x = x + 10 * leftRight;
y = y + 10 * upDown;
ball.style.top = y + "px";
ball.style.left = x + "px";
}
function startBouncing() {
setDim();
setInterval(bounce, 12);
}
</script>
</head>
<body onresize="setDim()" onload="startBouncing()">
<div class="content">
<h1>Bouncing ball in JavaScript <a href="./index.html">or CSS</a></h1>
<p>Javascript animation for a bouncing ball.</p>
<p>
Cast in shadow, falls,<br />Strikes the floor and leaps again,<br />Darkness
on the bounce.
</p>
<p>
Code:
<a href="https://github.com/evoluteur/bouncing-ball"
>evoluteur/bouncing-ball</a
>
at GitHub.
</p>
<div id="ball"></div>
<p>
<br />© 2020–2026
<a href="https://evoluteur.github.io/">Olivier Giulieri</a>
</p>
</div>
</body>
</html>