"Magnetic" Links Navbar Template
A sophisticated and modern navbar that creates an engaging micro-interaction.
As the user's cursor approaches a navigation link, the link animates and "snaps" to the cursor. A very modern and engaging micro-interaction.

The "Magnetic" Links Navbar
A sophisticated and modern navbar that creates an engaging micro-interaction. As the user's cursor moves over the navigation area, the links are subtly "pulled" towards the pointer, creating a playful magnetic effect that encourages interaction. This is ideal for creative sites, portfolios, and brands that want to showcase a high level of polish.
- Magnetic Link Animation — JavaScript tracks the mouse's position within the navigation bar and uses performant CSS
transform
properties to smoothly move each link towards the cursor. - Graceful Degradation — The magnetic effect is designed exclusively for mouse-based interaction. The script automatically detects if the user is on a touch-only device and disables the effect, defaulting to a standard, accessible navbar.
- Performant Implementation: Uses a single event listener on the parent navigation container to efficiently manage all link animations, avoiding performance issues. Links snap back into place on
mouseleave
. - Accessibility-Minded: The decorative motion is automatically disabled for users with the
prefers-reduced-motion
accessibility setting enabled in their operating system. On mobile, it functions as a standard, accessible hamburger menu.
Coding Best Practices
- CSS custom properties (variables) are used for easy theme customization (colors, fonts).
- The code is appropriately commented to explain the purpose of each section.
Code
Here's the full code for the navbar template: