-
Notifications
You must be signed in to change notification settings - Fork 2
/
mouse_trail.html
67 lines (58 loc) · 1.59 KB
/
mouse_trail.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
64
65
66
67
<!doctype html>
<style>
.trail { /* className for the trail elements */
position: absolute;
height: 6px; width: 6px;
border-radius: 3px;
background: teal;
}
body {
height: 300px;
}
</style>
<script>
/////////////////
// My solution //
/////////////////
let divs = [];
for (let i = 0; i < 10; i++) {
divs.push(document.createElement('div'));
divs[i].className = 'trail';
divs[i].style.display = 'none'
}
window.addEventListener('load', () => {
divs.forEach(div => {
document.body.appendChild(div)
})
});
// let lastX; // Tracks the last observed mouse X position
// let lastY; // Tracks the last observed mouse Y position
let i = 0;
const handleTrail = event => {
divs[i].style.top = (event.clientY - 5) + 'px';
divs[i].style.left = (event.clientX - 5) + 'px';
divs[i].style.display = 'block';
i++;
if (i >= divs.length) i = 0;
// lastX = event.clientX;
// lastY = event.clientY;
}
window.addEventListener('mousemove', handleTrail);
/////////////////////
// Book's solution //
/////////////////////
// let dots = [];
// for (let i = 0; i < 12; i++) {
// let node = document.createElement('div');
// node.className = 'trail';
// document.body.appendChild(node);
// dots.push(node);
// }
// let currentDot = 0;
// window.addEventListener('mousemove', event => {
// let dot = dots[currentDot];
// dot.style.top = (event.pageY - 3) + 'px';
// dot.style.left = (event.pageX - 3) + 'px';
// currentDot = (currentDot + 1) % dots.length;
// });
</script>