html
html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
<!-- Created by BroFar -->
<!-- revised by Chris Coder -->
<!DOCTYPE html>
<html>
<head>
<title>Magnet Simulation</title>
<link rel="stylesheet" href="style.css">
<meta name="viewport" content="width=device-width, initial-scale=1.0, shrink-to-fit=no">
</head>
<body>
<div class="magnet" id="magnet1"></div>
<div class="magnet2" id="magnet2"></div>
</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 BroFar */
.magnet{
width: 30px;
height: 50px;
background-color: #333;
border-radius: 5px;
position: absolute;
top: 100px;
left: 100px;
cursor: move;
}
.magnet2 {
width: 30px;
height: 50px;
background-color: #777;
border-radius: 5px;
position: absolute;
top: 100px;
left: 200px;
cursor: move;
}
#magnet1, #magnet2 {
/* ... other styles ... */
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
// Created by BroFar
//revised by Chris Coder
window.onload = function () {
var magnet1 = document.querySelector('#magnet1');
var magnet2 = document.querySelector('#magnet2');
function onTouchMove(event) {
var touch = event.targetTouches[0];
var x = touch.pageX - 15 + "px";
var y = touch.pageY -25 + "px";
// Update magnet positions
event.currentTarget.style.left = x;
event.currentTarget.style.top = y;
}
magnet1.addEventListener('touchmove', onTouchMove);
magnet2.addEventListener('touchmove', onTouchMove);
};
Enter to Rename, Shift+Enter to Preview
BROWSER
Console
Run