-
Notifications
You must be signed in to change notification settings - Fork 2
/
baloon.html
63 lines (58 loc) · 1.61 KB
/
baloon.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
53
54
55
56
57
58
59
60
61
62
63
<!doctype html>
<h1>a ballon which you can shrink or grow until explodes using arrow keys</h1>
<h1>PRESS <strong>⇵</strong> arrow keys to inflate or deflate this ballon.</h1>
<h2>Be careful! lol 😆</h2>
<p>🎈</p>
<script>
/////////////////
// My solution //
/////////////////
/* document.querySelector('p').style.fontSize = '30px';
const resizeBalloon = event => {
let balloon = event.target.querySelector('p');
let size = parseFloat(balloon.style.fontSize);
if (size >= 210) explode(balloon);
if (event.key === 'ArrowUp') inflate(balloon, size);
if (event.key === 'ArrowDown') deflate(balloon, size);
event.stopPropagation();
}
function inflate (balloon, size) {
balloon.style.fontSize = size + 10 + 'px'
}
function deflate (balloon, size) {
balloon.style.fontSize = size - 10 + 'px';
}
function explode (balloon) {
balloon.textContent = '💥';
window.removeEventListener('keydown', resizeBalloon);
}
window.addEventListener('keydown', resizeBalloon);
*/
/////////////////////
// Book's solution //
/////////////////////
let p = document.querySelector('p');
let size;
function setSize (newSize) {
size = newSize;
p.style.fontSize = size + 'px';
}
setSize(20);
function handleArrow (event) {
if (event.key === 'ArrowUp') {
if (size > 210) {
p.textContent = '💥';
document.body.removeEventListener('keydown', handleArrow);
}
else {
setSize(size * 1.1);
event.preventDefault();
}
}
else if (event.key === 'ArrowDown') {
setSize(size * 0.9);
event.preventDefault();
}
}
document.body.addEventListener('keydown', handleArrow);
</script>