forked from foocoding/JavaScript2
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmouse-move-example.html
More file actions
49 lines (45 loc) · 1.24 KB
/
mouse-move-example.html
File metadata and controls
49 lines (45 loc) · 1.24 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
<!DOCTYPE html>
<html>
<head>
<style>
body {
width: 100vw;
height: 100vh;
display: flex;
justify-content: center;
align-items: center;
cursor: none;
}
#box {
transition: height 1s, width 1s, background-color 1s;
background-color: lightseagreen;
width: 100px;
height: 100px;
position: absolute;
}
</style>
</head>
<body>
<div id="coordinates"></div>
<div id="box"></div>
<script>
const boxEl = document.getElementById('box');
boxEl.addEventListener('mousedown', () => {
boxEl.style.width = '500px';
boxEl.style.height = '500px';
boxEl.style.backgroundColor = 'coral';
});
boxEl.addEventListener('mouseup', () => {
boxEl.style.width = '100px';
boxEl.style.height = '100px';
boxEl.style.backgroundColor = 'lightseagreen';
});
document.body.addEventListener('mousemove', event => {
const coordEl = document.getElementById('coordinates');
coordEl.innerText = `X: ${event.pageX}, Y: ${event.pageY}`;
boxEl.style.left = `${event.pageX}px`;
boxEl.style.top = `${event.pageY}px`;
});
</script>
</body>
</html>