html
html
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 -->
<!-- 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">n<br><br>s</div>
<div class="magnet" id="magnet2">n<br><br>s</div>
<body>
</html>
<!-- revisions by chris coder -->
<!-- Removed inline onclick event handlers: -->
<!-- Changed CSS class for the second div: since both magnets share the styles -->
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 */
/* revised by Chris Coder */
.magnet {
width: 30px;
height: 50px;
border-radius: 5px;
position: absolute;
cursor: move;
box-shadow: 0px 4px 8px rgba(231, 20, 32, 0.5);
transition: transform 0.5s ease-in-out;
text-align:center;
font-size:11px;
}
.magnet.flip {
transform: rotate(90deg);
}
#magnet1 {
background-color: #333;
top: 10px;
left: 20px;
color: #fff;
}
#magnet2 {
background-color: #777;
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
// Code revisions by Chris Coder
window.onload = function () {
// Select magnets by their IDs
var magnet1 = document.querySelector('#magnet1');
var magnet2 = document.querySelector('#magnet2');
// Event handler for 'touchmove' event
function onTouchMove(event) {
event.preventDefault(); // prevents scrolling when moving the magnets // https://developer.mozilla.org/en-US/docs/Web/API/Event/preventDefault
// Get the first touch point (in case of multiple touches)
var touch = event.targetTouches[0];
// Calculate x-coordinate considering offset (-15)
var x = touch.pageX - 15;
// Calculate y-coordinate considering offset (-25)
Enter to Rename, Shift+Enter to Preview
BROWSER
Console
Run