Magnetic button
Mood track «Digital Lemonade» · Kevin MacLeod incompetech.com · licensed under Creative Commons BY 4.0
Botón magnético · Magnetic button · Magnetic hover · Pulsante calamita
As the cursor approaches, the button is "pulled" towards it, with its label following even further. Once the pointer leaves, it springs back into place.
- Era
- 2017 – today
- Use
- Mainstream
Live demo
View code
<main>
<p class="k">Studio Nebbia — contatti</p>
<h1>Hai un progetto<br>in mente?</h1>
<div class="zone"><button class="mag"><span>Scrivici →</span></button></div>
<i class="cur"></i>
</main>
<script>
if(matchMedia('(prefers-reduced-motion: no-preference)').matches){
const b=document.querySelector('.mag'),s=b.querySelector('span'),c=document.querySelector('.cur'),z=document.querySelector('.zone');
let t=0,mx=null,my=null,bx=0,by=0;
addEventListener('pointermove',e=>{mx=e.clientX;my=e.clientY});
document.addEventListener('pointerleave',()=>{mx=null});
const loop=()=>{t+=.013;const r=z.getBoundingClientRect(),cx=r.left+r.width/2,cy=r.top+r.height/2;
const x=mx??cx+Math.sin(t)*Math.min(innerWidth*.4,190),y=my??cy+Math.sin(t*2)*40;
const dx=x-cx,dy=y-cy,d=Math.hypot(dx,dy),near=d<120;
bx+=((near?dx*.42:0)-bx)*.12;by+=((near?dy*.42:0)-by)*.12;
b.style.transform=`translate(${bx}px,${by}px)`;s.style.transform=`translate(${bx*.45}px,${by*.45}px)`;
c.style.transform=`translate(${x}px,${y}px) scale(${near?2.6:1})`;b.classList.toggle('on',near);
requestAnimationFrame(loop)};loop();
}
</script> body{margin:0;height:100vh;overflow:hidden;background:#111014;color:#F3EFE6;font-family:Inter,sans-serif}
main{height:100%;display:flex;flex-direction:column;align-items:center;justify-content:center;text-align:center}
.k{margin:0 0 6px;font-size:12px;letter-spacing:.18em;text-transform:uppercase;color:#8A8794}
h1{font-family:Syne,sans-serif;font-weight:800;font-size:clamp(24px,6vw,38px);line-height:1.02;margin:0 0 22px}
.zone{width:240px;height:120px;display:grid;place-items:center}
.mag{width:112px;height:112px;border-radius:50%;border:0;background:#C6FF3D;color:#111014;font:500 15px Inter,sans-serif;cursor:pointer;box-shadow:0 0 0 1px #C6FF3D55,0 18px 40px -12px #C6FF3D66;transition:background .3s}
.mag span{display:inline-block}
.mag.on{background:#F3EFE6}
.cur{position:fixed;left:-6px;top:-6px;width:12px;height:12px;border-radius:50%;background:#F3EFE6;mix-blend-mode:difference;pointer-events:none;transition:transform .08s linear,scale .3s}
@media (prefers-reduced-motion:reduce){.cur{display:none}.mag:hover{background:#F3EFE6}} Characteristics
- Offset proportional to cursor distance
- Attraction radius larger than the button
- Inner label moving more than its container
- Elastic return to centre
- Typical of award-winning portfolios and agency sites
Palette
Click a swatch to copy it
Typography
Headings · Syne Magnetic button
Body · Inter As the cursor approaches, the button is "pulled" towards it, with its label following even further. Once the pointer leaves, it springs back into place.
Motion
- Easing
lerp 0.1–0.2 / spring o elastic.out(1, 0.3)- Typical duration
- continua mentre il cursore è vicino; 500–900 ms per il ritorno
- Techniques
- pointermove + getBoundingClientRect, transform translate su contenitore ed etichetta, GSAP quickTo, Framer Motion useSpring, requestAnimationFrame con interpolazione
Accessibility (reduced motion)
No displacement; the button only changes colour or contrast on hover.
Prompt for your agent
Copy and paste into your prompt to have an agent or image generator reproduce this style.
For web pages and UI
Design the web page or interface in this style: Magnetic button (Botón magnético). As the cursor approaches, the button is "pulled" towards it, with its label following even further. Once the pointer leaves, it springs back into place. Characteristics: Offset proportional to cursor distance, Attraction radius larger than the button, Inner label moving more than its container, Elastic return to centre, Typical of award-winning portfolios and agency sites. Palette: #111014, #F3EFE6, #C6FF3D, #2B2A30, #8A8794. Typography: headings in Syne, body text in Inter (Google Fonts or equivalents). Mood: playful, tactile, bold. Motion: easing lerp 0.1–0.2 / spring o elastic.out(1, 0.3), typical duration continua mentre il cursore è vicino; 500–900 ms per il ritorno; techniques: pointermove + getBoundingClientRect, transform translate su contenitore ed etichetta, GSAP quickTo, Framer Motion useSpring, requestAnimationFrame con interpolazione. With prefers-reduced-motion: No displacement; the button only changes colour or contrast on hover. References: Siti d'agenzia premiati su Awwwards, GSAP quickTo, Framer Motion useSpring, tutorial Codrops sui magnetic buttons.
Relations
Derives from
Similar to
References
- Siti d'agenzia premiati su Awwwards
- GSAP quickTo
- Framer Motion useSpring
- tutorial Codrops sui magnetic buttons