YL

BUTTON

磁吸按钮Magnetic Button

鼠标靠近时按钮像被磁铁吸住一样朝光标偏移,离开后弹性回位。

中英对照

中文
磁吸按钮
英文
Magnetic Button
别名
磁性悬停、磁力按钮、跟随按钮
一句话
鼠标靠近时按钮像被磁铁吸住一样朝光标偏移,离开后弹性回位。

效果预览

鼠标靠近时按钮会被"吸"过去

拖动参数实时改预览;复制提示词 / CSS 会带上当前数值。

适合用在哪

创意机构官网、作品集、高端品牌站;把"好玩"做进交互。

不适合:需要精准、可预期点击的地方(结账、表单提交、后台工具栏):按钮位置随光标变化会增加点击误差;多颗相邻按钮都带磁吸时,光标在它们之间移动会让几颗按钮同时晃动。

和相近模式的区别

按压按钮Press Button

按压按钮的反馈发生在点击那一刻(整颗下沉 4px),纯 CSS 的 :active 就能做,所有设备都有效;磁吸的反馈发生在点击之前(光标靠近时跟随),需要 JS 计算偏移,只在有精确指针的桌面端成立。

容易踩的坑

  • 偏移量超过 12px,按钮像在躲避光标,用户点击时容易点空。
  • mousemove 每次直接改 left/top 而不用 transform,会触发布局重排导致卡顿。
  • 触屏设备未用 (hover: hover) 检测降级,首次点击可能触发错位跳动。

设计提示词 · Prompt

磁吸按钮(Magnetic Button):监听按钮外层 80-120px 范围的 mousemove,计算光标与按钮中心的偏移向量,按钮按偏移量的 20-30% 跟随位移(translate),同时 text 内层可再乘 1.5 倍系数做二次视差。mouseleave 时用 spring 缓动回位——transition:cubic-bezier(.34,1.56,.64,1) 300ms 带回弹。按钮本身放大 5% 也不错。整体必须克制:偏移上限 8-12px,否则像 bug。桌面端专属,触屏设备自动降级为普通按钮。
Magnetic Button: listen for mousemove within an 80-120px area around the button, compute the offset vector between the cursor and the button center, and move the button by 20-30% of that offset (translate); the inner text layer can additionally multiply by a 1.5x factor for secondary parallax. On mouseleave, return with spring easing — transition:cubic-bezier(.34,1.56,.64,1) 300ms with overshoot. Scaling the button up 5% also works well. Keep it restrained overall: cap the offset at 8-12px, otherwise it looks like a bug. Desktop only; touch devices automatically fall back to a normal button.

可复制 CSS

自包含的最小实现,粘贴到空白页面即可生效;类名可以按项目规范改名。

/* <button class="magnetic-btn" type="button"><span class="magnetic-btn__label">和我聊聊</span></button>
   JS 写入 --mx / --my(单位 px,已限制在 ±10px 内),光标在感应范围内时加 .is-tracking */
.magnetic-btn {
  --mx: 0px;
  --my: 0px;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  height: 52px;
  padding: 0 30px;
  border: 0;
  border-radius: 999px;
  background: #111827;
  color: #fff;
  font: 600 15px/1 system-ui, sans-serif;
  cursor: pointer;
  transform: translate(var(--mx), var(--my));
  transition: transform .3s cubic-bezier(.34, 1.56, .64, 1);
}
.magnetic-btn__label {
  display: inline-block;
  /* 文字额外再走 0.5 倍,相对页面就是按钮偏移的 1.5 倍 */
  transform: translate(calc(var(--mx) * .5), calc(var(--my) * .5));
  transition: transform .3s cubic-bezier(.34, 1.56, .64, 1);
}
.magnetic-btn.is-tracking {
  transform: translate(var(--mx), var(--my)) scale(1.05);
  transition-duration: .15s;
}
.magnetic-btn.is-tracking .magnetic-btn__label { transition-duration: .15s; }
.magnetic-btn:focus-visible { outline: 2px solid #6366f1; outline-offset: 4px; }
@media (hover: none), (pointer: coarse), (prefers-reduced-motion: reduce) {
  .magnetic-btn, .magnetic-btn.is-tracking, .magnetic-btn__label { transform: none; transition: none; }
}

演示 HTML 结构

<div class="s d-magnet">
  <button class="d-magnet__btn" type="button">✦ 和我聊聊</button>
  <span class="d-tiny">鼠标靠近时按钮会被"吸"过去</span>
</div>

无障碍与性能

无障碍

  • 键盘聚焦时按钮保持原位,只显示焦点环;磁吸只是指针增强,不能承载任何信息。
  • prefers-reduced-motion: reduce 时关闭位移和缩放,按钮退化成普通按钮。

性能

  • 只写 transform,偏移通过 CSS 变量传入;pointermove 回调里用 requestAnimationFrame 合并,每帧最多写一次。
  • 不要在每次 pointermove 里调用 getBoundingClientRect():刚写完样式就读取几何信息会触发强制同步布局。在按钮没有偏移时(初始化、resize 和 scroll 之后)缓存中心点,或者测量外层不动的包裹元素。

常见问题

磁吸按钮的英文是什么?

磁吸按钮的英文是 Magnetic Button。

Magnetic Button 的中文是什么?

Magnetic Button 的中文是「磁吸按钮」。常见中文别名还包括 磁性悬停、磁力按钮、跟随按钮。鼠标靠近时按钮像被磁铁吸住一样朝光标偏移,离开后弹性回位。

磁吸按钮最少需要哪些 JS?

先用 matchMedia('(hover: hover) and (pointer: fine)') 和 prefers-reduced-motion 判断是否启用。启用时监听外层容器或 document 的 pointermove:算出光标到按钮中心的 dx、dy,距离小于约 100px 时把 dx × 0.25、dy × 0.25 限制在 ±10px 内写进 --mx / --my 并加 .is-tracking;超出范围或 pointerleave 时把两个变量归零、移除 .is-tracking,CSS 的回弹缓动负责回位。

为什么磁吸按钮会越动越抖?

通常是边移动边测量造成的。getBoundingClientRect() 返回的是应用 transform 之后的位置,按钮一偏移,测出来的中心也跟着偏,下一帧算出的偏移又变了,形成反馈抖动。按钮中心要在未偏移时缓存,或者测量外层不动的包裹元素。

常见搭配

同一页面里经常和「磁吸按钮」一起出现的模式,可以收藏后用「组合收藏为整页提示词」一起交给 AI。

标签