Magnetic Hover
A copy-paste buttons component in pure HTML, CSS & vanilla JS. Zero dependencies, framework-agnostic, MIT-licensed.
ButtonsHTMLCSSJavaScriptany framework
Copy into your project
HTML
<!-- Magnetic Hover — button content follows cursor slightly -->
<button class="nuda-magnetic">Magnetic Hover</button>CSS
/* ── Magnetic Hover ──────────────────────────────────────────
Customize:
--nuda-mg-bg : button background
--nuda-mg-clr : text color
--nuda-mg-radius : border radius
--nuda-mg-ease : return-to-center easing
──────────────────────────────────────────────────────────── */
.nuda-magnetic {
--nuda-mg-bg: #6366f1;
--nuda-mg-clr: #fff;
--nuda-mg-radius: 8px;
--nuda-mg-ease: cubic-bezier(0.23, 1, 0.32, 1);
padding: 12px 32px;
font-size: 1rem;
font-weight: 600;
color: var(--nuda-mg-clr);
background: var(--nuda-mg-bg);
border: none;
border-radius: var(--nuda-mg-radius);
cursor: pointer;
transition: transform 0.3s var(--nuda-mg-ease);
will-change: transform;
}JavaScript
/* ── Magnetic Hover — vanilla JS ─────────────────────────────
Shifts button content toward cursor position.
Respects prefers-reduced-motion.
──────────────────────────────────────────────────────────── */
;(function () {
if (window.matchMedia("(prefers-reduced-motion: reduce)").matches) return;
var strength = 0.35; /* 0–1, how strongly the button follows */
document.querySelectorAll(".nuda-magnetic").forEach(function (btn) {
btn.addEventListener("mousemove", function (e) {
var rect = btn.getBoundingClientRect();
var x = e.clientX - rect.left - rect.width / 2;
var y = e.clientY - rect.top - rect.height / 2;
btn.style.transform =
"translate(" + (x * strength) + "px, " + (y * strength) + "px)";
});
btn.addEventListener("mouseleave", function () {
btn.style.transform = "translate(0, 0)";
});
});
})();How to use Magnetic Hover
Paste the HTML where you need it and the CSS into a global stylesheet (or a <style> tag). Every class is prefixed nuda- so it never collides with Tailwind or your own styles. Tweak the CSS custom properties to match your design system.
Works in React, Vue, Svelte, Astro, Next.js, Nuxt, Laravel Blade, Django, Rails — or a single .html file. No npm install, no build step.