Magnetic Hover Button
Button that follows cursor with magnetic attraction effect
Embed this component
Paste this snippet into any site — the design renders in a sandboxed iframe served from /embed/magnetic-button.
<iframe src="/embed/magnetic-button" sandbox="allow-scripts" loading="lazy" title="Magnetic Hover Button" style="border:0;width:100%;height:480px;overflow:hidden"></iframe>Implementation code
Copy the HTML and CSS for Magnetic Hover Button, or grab a framework-specific implementation. The HTML and CSS variants are self-contained; framework variants assume that framework's project setup.
HTML
<button class="magnetic-button">Magnetic</button>CSS
.magnetic-button {
padding: 1rem 2rem;
border-radius: 0.75rem;
background: linear-gradient(to right, #6366f1, #9333ea);
color: white;
font-weight: 600;
border: none;
cursor: pointer;
transition: all 0.3s ease;
}
.magnetic-button:hover {
box-shadow: 0 20px 40px rgba(147, 51, 234, 0.5);
}React
import { useRef, useState } from 'react';
export default function MagneticButton() {
const buttonRef = useRef(null);
const [position, setPosition] = useState({ x: 0, y: 0 });
const handleMouseMove = (e) => {
const button = buttonRef.current;
const rect = button.getBoundingClientRect();
const x = e.clientX - rect.left - rect.width / 2;
const y = e.clientY - rect.top - rect.height / 2;
setPosition({ x: x * 0.2, y: y * 0.2 });
};
const handleMouseLeave = () => {
setPosition({ x: 0, y: 0 });
};
return (
<button
ref={buttonRef}
onMouseMove={handleMouseMove}
onMouseLeave={handleMouseLeave}
style={{
transform: `translate(${position.x}px, ${position.y}px)`,
transition: 'transform 0.3s ease'
}}
className="px-8 py-4 rounded-xl bg-gradient-to-r from-indigo-500 to-purple-600 text-white font-semibold"
>
Magnetic
</button>
);
}LWC — HTML template
<template>
<button
class="magnetic-button"
onclick={handleClick}
onmousemove={handleMouseMove}
onmouseleave={handleMouseLeave}
style={buttonStyle}
>
<slot>Magnetic</slot>
</button>
</template>LWC — JavaScript
import { LightningElement, api } from 'lwc';
export default class MagneticButton extends LightningElement {
@api label = 'Magnetic';
positionX = 0;
positionY = 0;
get buttonStyle() {
return `transform: translate(${this.positionX}px, ${this.positionY}px); transition: transform 0.3s ease;`;
}
handleMouseMove(event) {
const button = event.target;
const rect = button.getBoundingClientRect();
const x = event.clientX - rect.left - rect.width / 2;
const y = event.clientY - rect.top - rect.height / 2;
this.positionX = x * 0.2;
this.positionY = y * 0.2;
}
handleMouseLeave() {
this.positionX = 0;
this.positionY = 0;
}
handleClick() {
this.dispatchEvent(new CustomEvent('buttonclick', {
bubbles: true,
composed: true
}));
}
}LWC — CSS
.magnetic-button {
padding: 1rem 2rem;
border-radius: 0.75rem;
background: linear-gradient(to right, #6366f1, #9333ea);
color: white;
font-weight: 600;
border: none;
cursor: pointer;
}
.magnetic-button:hover {
box-shadow: 0 20px 40px rgba(147, 51, 234, 0.5);
}LWC — Configuration
<?xml version="1.0" encoding="UTF-8"?>
<LightningComponentBundle xmlns="http://soap.sforce.com/2006/04/metadata">
<apiVersion>59.0</apiVersion>
<isExposed>true</isExposed>
<targets>
<target>lightning__AppPage</target>
<target>lightning__RecordPage</target>
<target>lightning__HomePage</target>
</targets>
</LightningComponentBundle>Where Magnetic Hover Button fits
- Premium UI
- Interactive Elements
- Creative Sites
- Portfolios
Interaction states & animations
- Magnetic
- Hover
- Follow
Topics
- #magnetic
- #interactive
- #cursor
- #follow
- #premium
- #creative
Accessibility notes
Provide non-magnetic alternative for users with motor impairments. Ensure keyboard navigation works.
Browser support
- All modern browsers