xxxxxxxxxx
1
<body class="bg-dark">
2
<h1 class="text-center text-white mt-5">Move the cursor to animate the eyes</h1>
3
<div class="eyes">
4
<div class="eye">
5
<div class="ball"></div>
6
</div>
7
<div class="eye">
8
<div class="ball"></div>
9
</div>
10
</div>
11
</body>
xxxxxxxxxx
1
.eyes {
2
position: absolute;
3
top: 50%;
4
transform: translateY(-50%);
5
width: 100%;
6
text-align: center;
7
}
8
9
.eye {
10
width: 240px;
11
height: 120px;
12
background: #fff;
13
display: inline-block;
14
margin: 40px;
15
border-radius: 50%;
16
position: relative;
17
overflow: hidden;
18
}
19
20
.ball {
21
width: 50px;
22
height: 50px;
23
background: #000;
24
position: absolute;
25
top: 50%;
26
left: 50%;
27
transform: translate(-50%, -50%);
28
border-radius: 50%;
29
border: 10px solid #333;
30
}
xxxxxxxxxx
1
const balls = document.getElementsByClassName("ball");
2
document.onmousemove = function() {
3
let x = event.clientX * 100 / window.innerWidth + "%";
4
let y = event.clientY * 100 / window.innerHeight + "%";
5
// event.cilentX => get the horizontal coordinate of the mouse
6
// event.cilentY => get the vertical coordinate of the mouse
7
// window.innerWidth => get the browser width
8
// window.innerHeight => get the browser height
9
10
for(let i = 0; i < 2; i++) {
11
balls[i].style.left = x;
12
balls[i].style.top = y;
13
balls[i].style.transform = "translate(-"+x+", -"+y+")";
14
}
15
}
Console errors: 0