-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmousetrail.html
More file actions
51 lines (44 loc) · 1.45 KB
/
mousetrail.html
File metadata and controls
51 lines (44 loc) · 1.45 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
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8" />
<title>Mousetrail</title>
<meta name="viewport" content="width=device-width, initial-scale=1, shink-to-fit=no">
</head>
<body>
<style>
.trail {
/* className for the trail elements */
position: absolute;
height: 6px;
width: 6px;
border-radius: 3px;
background: teal;
}
body {
height: 300px;
}
</style>
<script>
'use strict';
const divArray = []; //create array to store dots
const body = document.querySelector('body');
document.addEventListener('mousemove', (e) => {
console.log('mouse coordinates: ', e.pageX, e.pageY);
let numberOfDots = 20;
if (divArray.length > numberOfDots) { //if over 20, remove the first dot
const removed = divArray.shift();
body.removeChild(removed);
} else {
let div = document.createElement('div');
div.className = 'trail'; //add classname for div
div.style.left = e.clientX + 'px'; //set coordinators
div.style.top = e.clientY + 'px';
document.body.appendChild(div);
divArray.push(div); //add div to array
}
}
);
</script>
</body>
</html>