<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>3D Magnet Effect Button</title>
<!-- Font Awesome for icons -->
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.7.2/css/all.min.css" />
<style>
* {
padding: 0;
margin: 0;
box-sizing: border-box;
}
html,
body {
width: 100%;
height: 100%;
position: relative;
}
body {
display: flex;
justify-content: center;
align-items: center;
height: 100vh;
background: linear-gradient(135deg, #dfe9f3, #f8e2cf, #f7d8e7);
font-family: Arial, Helvetica, sans-serif;
}
.btn {
display: inline-block;
padding: 15px 40px;
font-size: 20px;
font-weight: bold;
color: #fff;
background: linear-gradient(145deg, #3498db, #2980b9);
border: none;
border-radius: 15px;
cursor: pointer;
position: relative;
transform-style: preserve-3d;
box-shadow: 5px 5px 15px rgba(0, 0, 0, 0.136);
transition: background 0.3s ease;
}
.btn:hover {
background: linear-gradient(145deg, #4a9de6, #2c8dce);
}
</style>
</head>
<body>
<button class="btn"><i class="fas fa-magnet"></i> Hover Me</button>
<!-- GSAP CDN -->
<script src="https://cdnjs.cloudflare.com/ajax/libs/gsap/3.12.5/gsap.min.js"></script>
<!-- SCRIPT CODE -->
<script>
const btn = document.querySelector('.btn');
btn.addEventListener('mousemove', (e) => {
const { left, top, width, height } = btn.getBoundingClientRect();
const x = e.clientX - (left + width / 2);
const y = e.clientY - (top + height / 2);
// add tilt effect
let tiltVal = 20;
const rotateX = (y / height) * tiltVal;
const rotateY = (x / width) * -tiltVal;
let strength = 0.3;
gsap.to(btn, {
x: x * strength,
y: y * strength,
rotationX: rotateX,
rotationY: rotateY,
transformPerspective: 500,
transformOrigin: "center center",
duration: 0.3,
ease: "power2.out"
})
})
// return back the button to inital position
btn.addEventListener('mouseleave', () => {
gsap.to(btn, {
x: 0,
y: 0,
rotationX: 0,
rotationY: 0,
duration: 0.3,
ease: 'power2.out'
})
})
</script>
</body>
</html>
/* No CSS found */
// No JS found