005

Magnetic Cursor Effect

£7
or the whole Vault, £99
Open in new tab

Elements attract or repel from the cursor with elastic physics. Perfect for buttons, navigation items, and CTAs on award-winning sites.

intermediate
3 more details
cursor-effectsphysicsmicro-interaction

Updated

About this effect

An elastic magnetic attraction effect that makes elements respond to cursor proximity. Elements gently pull toward the mouse when it enters their magnetic field, creating a tactile, physics-based interaction. The strength, radius, and easing are all configurable per element. Used extensively on award-winning agency websites for buttons, navigation items, and interactive CTAs.

Magnetic Cursor Effect - GSAP animation effect preview

What's included

9 items
  • Configurable magnetic radius and attraction strength
  • Elastic easing that snaps elements back naturally
  • Works on any HTML element: buttons, links, images, cards
  • Per-element configuration via data attributes
  • Separate inner content movement for layered depth effects
  • Live magnetic-energy CSS variable for rings, borders and shadows
  • Elastic tap feedback for coarse-pointer devices
  • Pure GSAP implementation, no additional libraries
  • Keyboard focus and reduced-motion support

Perfect for

5 use cases
  • Interactive navigation menus and menu items
  • Call-to-action buttons with magnetic pull
  • Image galleries with hover attraction effects
  • Custom cursor interactions on portfolio sites
  • Interactive pricing cards and feature highlights

How it works

1 section

Each element has a pull radius taken from its resting position. Inside it the strength ramps up smoothly as the cursor closes in, driving movement, rotation, an optional counter-parallax on inner content, and a --magnetic-energy CSS variable you can hang your own styling off. The demo is a studio hero where every control carries a different setting: a light pull on the nav, an elastic primary button, a strong call disc with reversed inner parallax, and a scroll cue in repel mode. Attract and repel are the same code with the direction flipped, and leaving the field returns every channel home through your choice of smooth, snappy or elastic ease.

Difficulty Intermediate
Includes HTML + JS + CSS source, documentation, AI setup prompt, lifetime updates

Lighthouse, as measured

Google Lighthouse on this effect's demo, from the latest scan. A measurement of the demo as shipped, not a promise for your page.

Accessibility
100
Best practices
100

No performance score, on purpose. That figure depends on how you deploy: your server's compression and caching, your CDN, the connection and the device doing the test, none of which the code controls. The same page can score very differently on two consecutive runs, so measure it where it will live.

The Vault opens this source and the whole library.

Each product gives you the HTML, CSS and JavaScript source, the full documentation and an AI setup prompt, licensed for unlimited personal and commercial projects. Every paid product is tested for desktop and mobile layouts, reduced-motion handling, animation cleanup and keyboard controls wherever there is something to operate.

One payment, over 300 products, and future additions to the library.

Documentation

Quick Start

1. Add GSAP and the effect before the closing </body> tag:

In your downloadThe working code for this step, in index.html.

2. Add the magnetic attribute to an interactive element:

In your downloadThe working code for this step, in index.html.

Using It With Your Own Design

The effect needs nothing from your markup beyond the data-magnetic attribute on an element that can move, and it reads every setting from the data attributes below. Everything else in the demo (the studio nav, the headline, the call disc, the scroll cue) is ordinary page content styled in assets/style.css and can be deleted or replaced.

What the effect does depend on:

  • [data-magnetic] must be transformable. Inline elements cannot be translated, so give links display: inline-flex (or inline-block). The demo's [data-magnetic] { position: relative; will-change: transform; } rule is the only structural CSS.
  • Resting positions are measured, not tracked. The field centre is the element's layout position at init, re-measured on resize, scroll and once web fonts have loaded. If you move an element by other means (a layout change, an opened panel) call window.magneticCursor.refresh().
  • data-magnetic-inner selects one descendant for the second movement layer. It is a normal querySelector inside the element, so any class of your own works; the demo's .btn__label and .hero__disc-content are demo names, not contract.
  • The effect writes --magnetic-energy (0 to 1) and toggles .is-magnetic-active on each element while the pointer is inside its field. Both are optional hooks: the demo brightens a hairline ring around the disc and the border on the buttons with them. Declare --magnetic-energy: 0 on the element so its CSS has a value before the pointer arrives.
  • No custom cursor is drawn. The demo uses the system pointer; if you add a custom cursor it is independent of this effect.
  • Coarse pointers get real buttons. On touch devices the proximity loop never starts and each element receives a short elastic tap response instead, so every magnetic element should be a native link or button that works without the movement.

Options

Attribute Values Default Description
data-magnetic-mode attract, repel attract Pulls toward or pushes away from the pointer
data-magnetic-strength Pixels 30 Maximum element translation
data-magnetic-radius Pixels 200 Activation radius around the resting center
data-magnetic-ease elastic, smooth, snappy smooth Tracking and return character
data-magnetic-inner CSS selector — Descendant that receives a second movement layer
data-magnetic-inner-strength Pixels 10 Maximum inner translation
data-magnetic-inner-reverse Presence — Moves inner content opposite the outer element

CSS Requirements

In your downloadThe working code for this step, in style.css.

The effect writes --magnetic-energy from 0 to 1. Use it to drive rings, borders, shadows, or other visual responses. The demo's call disc sharpens a hairline ring with it:

In your downloadThe working code for this step, in style.css.

Accessibility and Devices

  • The proximity effect runs only with a fine pointer.
  • Keyboard focus mirrors activation with a directional lift and inner response.
  • Coarse-pointer devices receive a short elastic tap response rather than a hover dependency.
  • Reduced motion keeps every control static and fully usable.
  • Use native links and buttons so activation never depends on the visual effect.

Dependencies

  • GSAP 3.15.0+

Also in your download: the full source, worked examples, the events and programmatic API, and the class reference.

£7