html
html
1
2
3
4
5
6
7
8
9
10
11
12
13
<!-- created by JaScript
Please upvote if you like -->
<!DOCTYPE html>
<html>
<head>
<title>Combine event with movement</title>
</head>
<body>
<div id="field"><span id="handling"><h1>click or touch in gray area</h1></span>
<div id="box"></div>
</div>
</body>
</html>
Enter to Rename, Shift+Enter to Preview
css
css
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
/* created by JaScript
Please upvote if you like */
body {
width: 100vw;
background-color: black;
position: fixed;
user-select: none;
-webkit-user-select: none;
}
#field {
width: 97.6vw;
height: 50vh;
background-color: gray;
position: relative;
}
#box {
width: 3em;
height: 3em;
background-color: lime;
top: 3.8em;
left: 3.8em;
position: absolute;
border-radius: 3em;
}
#field, #box {
border-style: solid;
color: #ffbb99;
}
Enter to Rename, Shift+Enter to Preview
js
js
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
/* created by JaScript
Please upvote if you like */
function moving(){
let x, y, kx=90, ky=90;
let field = document.getElementById('field');
field.addEventListener('mousemove', handleMouseMove, false);
let started = false;
function handleMouseMove(e){
x = e.clientX;
y = e.clientY;
var intervalId = setInterval(function(){
move();
},20);
}
let box = document.getElementById('box');
function move() {
if(Math.abs(ky - y)>3){
if(y > ky){
ky += 1;
} else {
ky -= 1;
}
box.style.top = ky-30 + 'px';
}
if(Math.abs(kx - x)>3){
if(x > kx){
kx += 1;
} else {
Enter to Rename, Shift+Enter to Preview
BROWSER
Console
Run