A magnetic button hover effect built with CSS custom properties and a few lines of vanilla JavaScript. When the mouse comes close, the button leans toward the cursor, its label follows a little further, and both spring back when the pointer moves away. Use it for hero calls to action, play buttons or portfolio links that should feel playful.
How it works
The root section.mgb-stage contains an intro and a .mgb-row of three .mgb-field wrappers. Each field holds one .mgb-btn (a solid pill, a ghost outline and a round play button) with an inner .mgb-label.
The CSS moves the button with transform: translate3d(var(--mgb-x), var(--mgb-y), 0) and moves the label by half of the same offset, which creates a subtle parallax inside the button. The script listens to pointermove on the document and batches work with requestAnimationFrame. For each field it measures the distance between the cursor and the field centre (the field never moves, so this is the button's resting position). If the cursor is within data-mgb-radius pixels of the button edge, it sets --mgb-x and --mgb-y to the distance multiplied by data-mgb-strength and adds .is-active, which shortens the transition so the button follows the pointer closely. Outside the radius the offsets return to zero and the --mgb-ease curve adds a small overshoot as the button snaps back.
The effect only runs for a mouse on devices with a fine pointer. Touch users, keyboard users and visitors without JavaScript get normal static buttons.
Customization
data-mgb-radiuson.mgb-field: how many pixels around the button the pull starts, e.g.150for a wider field.data-mgb-strengthon.mgb-field: how far the button follows the cursor;0.2is subtle,0.6is strong.--mgb-returnon.mgb-stage: how long the button takes to spring back to its place.--mgb-ease: the snap back curve; a value above 1 in the cubic bezier produces the overshoot.--mgb-accentand--mgb-accent-ink: the lime color of the solid button and its text color.- Change the
0.5multiplier in the.mgb-labeltransform to make the label parallax stronger or remove it. - Use
.mgb-btn--solid,.mgb-btn--ghostor.mgb-btn--roundon any link or button to pick a style.
Use cases
- Hero sections: a call to action leans toward the cursor and springs back when the pointer leaves
- Portfolio sites: ghost and solid pill links feel playful with a subtle label parallax
- Video landing pages: a round play button pulls toward the mouse as it approaches
- Creative agency sites: the effect runs only for fine pointers, so touch visitors see static buttons
Accessibility
The buttons are real a and button elements, so keyboard and screen reader behavior is unchanged. The icon-only play button has an aria-label, and its SVG is hidden from assistive technology. Every button shows a visible focus outline. Under prefers-reduced-motion: reduce the script skips the effect and the CSS removes all transforms.
Usage notes
Any number of fields can be placed on a page; the single document listener handles all of them. Leave some space around each button so the moving button does not overlap neighbors.