Quickly navigate through the dashboard with your keyboard using the command menu.
A primary CTA whose label and soft accent halo are magnetically pulled toward the cursor inside its hit area, then spring back on leave.
Magnetic Button is a primary call-to-action whose label and a soft accent halo are gently tugged toward your cursor while it hovers inside the button's hit area, then spring back home when you leave. It feels playful and physical, like the button is reaching out to meet you. Reach for it on the one action you really want people to click — a hero CTA, a signup, a "Launch project" button.
This gem is mostly CSS with a small pointer script — no libraries, no build step.
index.html, assets/style.css, and assets/script.js into one folder.assets/byq-brand.css and assets/gem-frame.css alongside them.index.html in any modern browser.This gem ships no images, so there's no assets/medias/ folder to copy, and it lists
no GSAP dependency — everything runs on plain CSS and vanilla JavaScript. The demo
links assets/byq-brand.css, which supplies the BYQ brand tokens (colors, fonts,
radii) used throughout; keep that stylesheet on the page so the tokens resolve.
The whole effect lives inside one namespaced <section>. It's a single link dressed
up as a pill, with a separate halo layer behind it.
<section class="byq-gem byq-gem--magnetic-button-01">
<div class="byq-gem--magnetic-button-01__actions">
<a class="byq-gem--magnetic-button-01__magnet" href="#" data-magnet>
<span class="byq-gem--magnetic-button-01__halo" aria-hidden="true"></span>
<span class="byq-gem--magnetic-button-01__label" data-magnet-inner>
<span class="byq-gem--magnetic-button-01__label-text">Launch project</span>
<svg class="byq-gem--magnetic-button-01__arrow" viewBox="0 0 24 24" fill="none" aria-hidden="true">
<path d="M5 12h13M12 5l7 7-7 7" stroke="currentColor" stroke-width="2"
stroke-linecap="round" stroke-linejoin="round" />
</svg>
</span>
</a>
</div>
</section>The <a> marked data-magnet is the enlarged hit area the script listens to. Inside
it, the __halo span is the glow that gets pulled, and the __label pill holds the
text and arrow. The data-magnet / data-magnet-inner attributes are the hooks the
JavaScript uses to find its targets.
Every selector is prefixed with .byq-gem--magnetic-button-01 so nothing leaks into
the rest of your page. The magnet element is the hit area, and it declares the custom
properties the effect rides on.
.byq-gem--magnetic-button-01__magnet {
--mx: 0px;
--my: 0px;
--pull-label: 0.35; /* label follows the cursor a touch less than the halo */
position: relative;
display: inline-flex;
padding: 1.35rem 1.15rem; /* generous invisible hit padding */
margin: -0.6rem; /* absorb padding so layout stays tight */
}The --mx / --my variables are the pull offset in pixels — the script writes them.
The generous padding grows the invisible hit area so the magnet starts pulling
before the cursor touches the visible pill; the negative margin cancels that padding
so layout stays tight.
The halo and label both translate by those variables, but by different amounts, which
sells the depth. The halo moves 1.35× further than the raw offset; the label only
--pull-label (0.35) of it.
.byq-gem--magnetic-button-01__halo {
transform: translate(-50%, -50%)
translate(calc(var(--mx) * 1.35), calc(var(--my) * 1.35));
transition: transform 0.55s cubic-bezier(0.16, 1, 0.3, 1), opacity 0.45s ease;
}
.byq-gem--magnetic-button-01__label {
transform: translate(
calc(var(--mx) * var(--pull-label)),
calc(var(--my) * var(--pull-label))
);
transition: transform 0.55s cubic-bezier(0.16, 1, 0.3, 1),
background 0.4s ease, border-color 0.4s ease;
}While the pointer is moving, the script adds .is-pulling, which shortens the
transition so the pull tracks instantly — but the longer spring stays for the return.
.byq-gem--magnetic-button-01__magnet.is-pulling .byq-gem--magnetic-button-01__halo,
.byq-gem--magnetic-button-01__magnet.is-pulling .byq-gem--magnetic-button-01__label {
transition-duration: 0.12s;
}On :hover / :focus-visible the pill fills with var(--byq-accent-lime), the halo
fades in, and the arrow nudges right. A prefers-reduced-motion block zeroes the
transforms so the button holds still for readers who ask for less movement.
The script's only job is to turn the cursor's position into the --mx / --my
offsets. It's scoped to this gem and depends on nothing.
First it grabs the gem root, finds the magnet by its data-magnet hook, and sets
PULL — the maximum distance (in px) the halo may travel from center.
const root = document.querySelector(".byq-gem--magnetic-button-01");
const magnet = root.querySelector("[data-magnet]");
const reduce = window.matchMedia("(prefers-reduced-motion: reduce)");
const PULL = 26;On each pointermove, it measures the cursor's offset from the button's center and
normalizes it to a −1…1 range using getBoundingClientRect(), so the pull scales with
the button's size rather than raw pixels.
function onMove(e) {
if (reduce.matches) return;
const r = magnet.getBoundingClientRect();
const nx = (e.clientX - (r.left + r.width / 2)) / (r.width / 2);
const ny = (e.clientY - (r.top + r.height / 2)) / (r.height / 2);Those fractions get clamped to −1…1 so a fast pointer can't fling the halo past its
intended reach, then multiplied by PULL and written onto the element as --mx /
--my. Adding .is-pulling first makes CSS track the cursor without lag.
const cx = Math.max(-1, Math.min(1, nx));
const cy = Math.max(-1, Math.min(1, ny));
magnet.classList.add("is-pulling");
magnet.style.setProperty("--mx", `${cx * PULL}px`);
magnet.style.setProperty("--my", `${cy * PULL}px`);
}Finally, reset() drops the class and zeroes both offsets, wired to pointerleave
and blur so the button springs home whether you move the mouse away or tab off. A
click handler calls preventDefault() to keep the placeholder link tidy.
magnet.addEventListener("pointermove", onMove);
magnet.addEventListener("pointerleave", reset);
magnet.addEventListener("blur", reset);
magnet.addEventListener("click", (e) => e.preventDefault());PULL in script.js (default 26). Higher makes the halo
reach further toward the cursor.--pull-label on .byq-gem--magnetic-button-01__magnet (default
0.35) — raise it and the pill chases the cursor more closely.1.35 multiplier on the __halo transform sets how far the glow
overshoots the label. Bigger exaggerates the parallax.var(--byq-accent-lime); swap it for
var(--byq-accent-solar) to re-skin the button.__label-text span ("Launch project") and drop the __arrow
SVG if you don't want the chevron.0.55s cubic-bezier(0.16, 1, 0.3, 1) transition on the halo
and label to make the spring-back snappier or looser.