中文
北煜ii全栈工程师 · 做网站的人网站小程序AI 应用
Hover & pointer · Vanilla JS + CSS

3D tilt

3D 跟随倾斜

Tilt a surface toward the pointer.

Also searched as: 卡片跟着鼠标立体地倾斜, 鼠标在卡片上移动,卡片往那边歪过去, 卡片有立体感地转向鼠标, 视差倾斜卡片, 3D 倾斜

AI Markdown (.md)
Placement
Card list, Image
Mood
Refined, Playful
Trigger
悬停
Site types
作品集, 个人网站, 品牌官网

How it works & accessibility

Normalize pointer coordinates within the card and map them to rotateX and rotateY under perspective.

Best for: Card list, Image (悬停).

Reduced motion: 不倾斜,卡片保持平的(跟着鼠标的 3D 转动容易让人晕)

Mobile & touch: 手机上没有悬停:手指按住时朝按的地方倾斜,松开回正;手指拖动照常滚动页面

Key parameters

ParameterKeyDefaultRangeNotes
边上倾斜的角度angle8°——
透视距离perspective1000px——
跟随和回正的时长duration300ms——
缓动easingcubic-bezier(0.16, 1, 0.3, 1)——

Ready-to-paste AI prompt

Add “3D tilt” to the card list in my project: Normalize pointer coordinates within the card and map them to rotateX and rotateY under perspective.
Parameters: angle: 8°, perspective: 1000px, duration: 300ms, easing: cubic-bezier(0.16, 1, 0.3, 1).
Preserve existing content and layout. Respect prefers-reduced-motion by showing the final state. Content must remain visible if JavaScript fails or is disabled.
Reference: https://donglang.linter.top/effects/tilt-3d.md (canonical specification and reference code).

Dependency-free reference code

CSS
/* 3D 跟随倾斜(3D tilt)的样式,配合 tilt-3d.js。脚本给卡片加上 class="tilt",再按参数设置下面这几个变量:
   --tilt-x、--tilt-y 是绕横轴、绕竖轴转的角度(跟着鼠标变),--tilt-perspective 是透视距离,--tilt-duration、--tilt-easing 是跟随和回正的过渡。
   透视写在卡片自己的 transform 里(perspective()),不用给外面套一层;只动 transform,不引起重新排版。
   卡片原来的 transform 会被盖掉;想倾斜时顺便放大一点,在这一行最后加 scale(1.03)。
   卡片原来的 transition 也会被盖掉(transition 只能写一行,后写的盖掉先写的):原来就有过渡的(比如悬停变色),把原来的几项并进下面这一行。
   卡片里的内容想浮在卡片上面(更立体),给卡片加 transform-style: preserve-3d、给内容加 translate: 0 0 30px;
   卡片自己不能有 overflow: hidden,不然里面的立体层会被压平。 */
.tilt {
  transform: perspective(var(--tilt-perspective, 1000px)) rotateX(var(--tilt-x, 0deg)) rotateY(var(--tilt-y, 0deg));
  transition: transform var(--tilt-duration, 300ms) var(--tilt-easing, ease-out);
}
JavaScript
// 3D 跟随倾斜(3D tilt)· 原生 JS,不依赖任何库。
// 改写自 Animation Handbook 的 Parallax 3D Tilt(MIT License,Copyright (c) 2026 Matin Monshizadeh):做法来自它——
// 按鼠标在卡片上的位置算出绕横轴、绕竖轴各转多少,鼠标靠哪边哪边往里压,离开时回正。StyleKit 的 tilt-3d(MIT)是同一个算法。
// cards 是一张或一组卡片(配合 tilt-3d.css)。鼠标在卡片上移动时,卡片朝鼠标那边倾斜,鼠标在边上时倾斜 angle 度(在角上两个方向各 angle 度);
// 鼠标离开时回正。跟着鼠标和回正都用 CSS 过渡(duration、easing),鼠标停下时卡片慢慢跟上。
// 返回 { tilt(card, x, y), reset(card), destroy() }:x、y 是鼠标在卡片上的位置,从 -0.5(左边、上边)到 0.5(右边、下边)。
// 卡片原来的 transform 会被盖掉(倾斜写在 transform 里);想同时放大,把 scale() 加在 tilt-3d.css 那一行的最后。
// 开启"减少动态"时:什么都不做,卡片保持平的(跟着鼠标的 3D 转动容易让人晕)。脚本没执行时卡片照常显示,只是不倾斜。
// 手机上没有悬停:手指按住时朝按的地方倾斜,松开回正;手指拖动照常滚动页面(浏览器接手滚动时会发 pointercancel,卡片回正)。
// listen 为 false 时不监听鼠标,只用 tilt / reset 自己控制(演示用)。ignoreReducedMotion 只给演示用,网站自己的卡片不要打开它。
export function tilt3d(cards, {
  angle = 8,
  perspective = 1000,
  duration = 300,
  easing = 'cubic-bezier(0.16, 1, 0.3, 1)',
  listen = true,
  ignoreReducedMotion = false,
} = {}) {
  // 减少动态时什么都不做:卡片保持原样
  if (!ignoreReducedMotion && matchMedia('(prefers-reduced-motion: reduce)').matches) return { tilt() {}, reset() {}, destroy() {} };
  const list = cards instanceof Element ? [cards] : [...cards];
  const edge = (v) => Math.max(-0.5, Math.min(0.5, v));

  // 鼠标靠哪边哪边往里压:在上边时绕横轴正转(上边往里),在右边时绕竖轴正转(右边往里)
  const tilt = (card, x, y) => {
    card.style.setProperty('--tilt-x', `${(-2 * edge(y) * angle).toFixed(2)}deg`);
    card.style.setProperty('--tilt-y', `${(2 * edge(x) * angle).toFixed(2)}deg`);
  };
  const reset = (card) => {
    card.style.setProperty('--tilt-x', '0deg');
    card.style.setProperty('--tilt-y', '0deg');
  };
  // 鼠标在卡片上的位置(-0.5 到 0.5)。卡片倾斜后画面上的框会变一点点(几度时不到 1%),直接用它
  const local = (card, e) => {
    const r = card.getBoundingClientRect();
    return [(e.clientX - r.left) / r.width - 0.5, (e.clientY - r.top) / r.height - 0.5];
  };
  const listeners = (card) => {
    let touching = false;
    return [
      ['pointermove', (e) => { if (e.pointerType !== 'touch' || touching) tilt(card, ...local(card, e)); }],
      ['pointerleave', (e) => { if (e.pointerType !== 'touch') reset(card); }],
      ['pointerdown', (e) => { if (e.pointerType === 'touch') { touching = true; tilt(card, ...local(card, e)); } }],
      ['pointerup', (e) => { if (e.pointerType === 'touch') { touching = false; reset(card); } }],
      ['pointercancel', () => { touching = false; reset(card); }],
    ];
  };

  const state = new Map();
  list.forEach((card) => {
    card.classList.add('tilt');
    card.style.setProperty('--tilt-perspective', `${perspective}px`);
    card.style.setProperty('--tilt-duration', `${duration}ms`);
    card.style.setProperty('--tilt-easing', easing);
    reset(card);
    const events = listen ? listeners(card) : [];
    events.forEach(([type, fn]) => card.addEventListener(type, fn));
    state.set(card, events);
  });

  return {
    tilt,
    reset,
    destroy() {
      state.forEach((events, card) => {
        events.forEach(([type, fn]) => card.removeEventListener(type, fn));
        card.classList.remove('tilt');
        ['--tilt-x', '--tilt-y', '--tilt-perspective', '--tilt-duration', '--tilt-easing'].forEach((name) => card.style.removeProperty(name));
      });
      state.clear();
    },
  };
}
HTML
<!-- 3D 跟随倾斜:把要倾斜的卡片交给它(一屏一两张就够),脚本会给卡片加上 class="tilt" -->
<div class="works">
  <a class="cover" href="/works/poster" data-tilt>
    <img src="poster.jpg" alt="海报设计">
  </a>
  <a class="cover" href="/works/app" data-tilt>
    <img src="app.jpg" alt="记账小程序">
  </a>
</div>
<script type="module">
  import { tilt3d } from './tilt-3d.js';
  tilt3d(document.querySelectorAll('[data-tilt]'));
</script>

More in Hover & pointer

Browse all 86 effects →

Shown once per release · Always available in changelog