html
html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
<!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" onclick="id='flip'">n<br><br>s</div>
<div class="magnet2" id="magnet2" onclick="id='flip2'">n<br><br>s</div>
<script src="script.js"></script>
</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
div {
text-align:center;
font-size:13px;
}
.magnet{
width: 30px;
height: 50px;
background-color: #333;
border-radius: 5px;
position: absolute;
cursor: move;
color: #fff;
}
.magnet2 {
margin-left:75px;
width: 30px;
height: 50px;
background-color: #777;
border-radius: 5px;
position: absolute;
cursor: move;
color: #000;
}
#magnet1, #magnet2 {
/* ... other styles ... */
box-shadow: 0px 4px 8px rgba(231, 20, 323, 0.5);
}
#flip{
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
function detectBrowser() {
var ua = navigator.userAgent;
if (ua.indexOf("Chrome") > -1) {
return "Chrome";
} else if (ua.indexOf("Firefox") > -1) {
return "Firefox";
} else if (ua.indexOf("Safari") > -1) {
return "Safari";
} else if (ua.indexOf("Opera") > -1) {
return "Opera";
} else if (ua.indexOf("MSIE") > -1) {
return "Internet Explorer";
} else {
return "Unknown";
}
}
var browser = detectBrowser();
console.log("Browser:", browser);
onload = function () {
magnet1.ontouchmove = function (x) {
touch = x.targetTouches[0];
// Update magnet1 position with collision detection
var newLeft = touch.pageX - 15 + "px";
var newTop = touch.pageY - 25 + "px";
magnet1.style.left = getNewPosition(newLeft, magnet2);
Enter to Rename, Shift+Enter to Preview
BROWSER
Console
Run