How to use it
1. Add the two files
<link rel="stylesheet" href="magnetic.css"> ... <script src="magnetic.js"></script>
2. Write a button
The class mbtn gives the look. The attribute data-magnetic gives the pull. The inner span makes the text move a little more than the button, which adds depth.
<a class="mbtn" data-magnetic href="/contact"> <span class="mbtn-label">Get started</span> </a>
3. Pick a style
<a class="mbtn" data-magnetic>Filled</a> <a class="mbtn mbtn--ghost" data-magnetic>Outline</a> <a class="mbtn mbtn--glow" data-magnetic>Glowing</a>
Change the colours
Everything is driven by CSS variables. Set them once for the whole site, or on a single button.
:root {
--mbtn-a: #ff5c8a; /* gradient start */
--mbtn-b: #ffb86b; /* gradient end */
--mbtn-text: #ffffff; /* text colour */
--mbtn-radius: 14px; /* corner radius */
}All options
| Setting | What it does |
|---|---|
data-magnetic | Switches the pull on, with a strength of 0.35. |
data-magnetic="0.6" | Sets the strength, from 0.1 for gentle to 1 for strong. |
.mbtn--ghost | Outline style with a gradient border. |
.mbtn--glow | Adds a soft pulse around the button. |
.mbtn-label | Optional inner span. It moves a little extra for a layered feel. |
Where to use it
- On the one button that matters. The magnetic pull tells the visitor which button is the main one.
- Not on every link. If everything moves, nothing stands out.
- The ripple works without the pull. Any
.mbtngets the click ripple, also on phones.
Common questions
Does it work on real buttons and on links?
Yes, both button and a elements work.
What about keyboard users?
The buttons keep a clear focus outline. The pull is for mouse users only, and the button always stays clickable.
What is in the download
magnetic.jsthe pull and the ripple, about 2 KBmagnetic.cssthree button styles, about 2 KBindex.htmlanddemo.cssthe demo page you see aboveREADME.txtandLICENSE.txtthe short guide and the MIT licence
Licence
MIT licence. Use it in personal and commercial projects, change it as you like. A link back is welcome and not required.