EN
作品全部设计开发
个人网站海报小程序插画小工具图标
页面与布局 · 原生 JS + CSS

滑动重排

FLIP layout animation

筛选、排序时,每张卡片从原来的位置滑到新位置,不是一下子跳过去

常见大白话搜法:筛选时卡片滑到新位置、点分类以后剩下的卡片平滑补位、列表排序时每一项挪过去、换布局时卡片不是一下子跳过去、布局重排

AI 专用 Markdown (.md)
适用位置
卡片列表
气质感觉
利落、柔和
触发方式
点击
适合网站
作品集、个人网站、博客

实现原理与无障碍底线

改布局之前记下每一项在哪,改完再量一次新位置;先用位移把每一项挪回原来的地方,再让位移归零,看起来就是滑过去的(FLIP:记起点、量终点、反推回起点、播放)。新出现的淡入,筛掉的在原地淡出。

适用场景:作品集按类别筛选、文章按标签过滤、列表排序、切换几列显示

减少动态(prefers-reduced-motion):直接换成新的布局,不滑动、不淡入淡出

移动端与触屏:和桌面一样;手机上一列排开时卡片挪的距离长,时长可以短一点

关键可调参数

参数键名默认值建议范围说明
时长duration500ms——
缓动easingcubic-bezier(0.2, 0.7, 0.3, 1)——

可直接复制给 AI(Cursor / Claude / v0)的提示词

请在我的项目里给卡片列表加上「滑动重排」(FLIP layout animation)效果:改布局之前记下每一项在哪,改完再量一次新位置;先用位移把每一项挪回原来的地方,再让位移归零,看起来就是滑过去的(FLIP:记起点、量终点、反推回起点、播放)。新出现的淡入,筛掉的在原地淡出。
参数:时长 500ms,缓动 cubic-bezier(0.2, 0.7, 0.3, 1)。
要求:保留原有的内容和布局;系统开启"减少动态"时直接显示最终状态;脚本出错或没有执行时内容仍然可见。
参考:https://donglang.linter.top/effects/layout-flip.md(原理、参数和参考代码)

原生无依赖参考代码

CSS
/* 滑动重排(FLIP layout animation):做法全在 layout-flip.js 里,不需要额外的样式。要注意的几点:
   - 筛掉的项用 hidden 属性(或 display: none)藏起来,留在框里,才会在原地淡出;直接从页面上删掉的项没法淡出。
   - 项自己的 CSS 写了 display(比如 display: grid、flex)时,hidden 属性会失效(作者写的样式盖过浏览器自带的 [hidden] { display: none }),
     筛掉的项不会消失:加一句 [hidden] { display: none !important; }。
   - 每一项要是框的直接子元素;项里面的东西跟着项一起动。
   - 框是网格(grid)、项的大小不变时最自然;换成一列这类连大小一起变的,大小会直接变过去,只有位置是滑过去的。
   - 筛掉一些以后框会直接变矮,框下面的内容(还有框在竖直方向居中时框上面的内容)会一下子挪过去:
     不想这样就给框留出固定的高度(比如网格写死几行:grid-template-rows: repeat(2, 240px))。 */
JavaScript
// 滑动重排(FLIP layout animation)· 原生 JS,不依赖任何库。
// 改写自 Animation Handbook 的 FLIP Technique(MIT License,Copyright (c) 2026 Matin Monshizadeh):改布局之前记下每一项的位置(First),
// 改布局(Last),用位移把每一项挪回原来的地方(Invert),再让位移归零(Play),看起来就是滑过去的;只动位移,不引起重新排版。
// 默认的时长、缓动来自它。
// 用法:container 是放这些项的框,它的直接子元素就是"每一项";改布局的代码(筛选、排序、换几列)放进 change,同步改完:
//   layoutFlip(grid, () => cards.forEach((c) => { c.hidden = c.dataset.tag !== tag; }));   // 筛选
//   layoutFlip(grid, () => sorted.forEach((c) => grid.append(c)));                      // 排序
// - 新出现的项(原来藏着的、新加进来的)在新位置淡入,从 0.9 倍放大到原样;
// - 筛掉的项(用 hidden 或 display: none 藏起来、还在框里的)在原地淡出、缩小,播完才真的藏起来;淡出时它不占位置,别的项直接往空出来的地方滑。
//   从框里删掉的项没法淡出,直接消失;
// - 只滑位置:项的大小变了(比如换成一列)时,大小直接变过去;要连大小一起变,参考共享元素过渡的缩放写法;
// - 框的高度直接变成新的,框下面的内容会一下子挪过去;
// - 正在滑的时候又调用(连点筛选):从现在看到的位置接着滑,不会先跳回去;
// - 位移、缩放叠加在项原来的 translate、scale 上(比如悬停上浮),不盖掉它们;
// - 每调用一次要把每一项的位置量两遍(改之前、改之后),几十项没问题;到几百项会卡一下,只对屏幕里的项做(屏幕外的直接换),或者分页。
// 返回的 Promise 在这一次都播完(或者被下一次打断)时完成。
// 开启"减少动态"时:直接换成新布局,不滑动、不淡入淡出。ignoreReducedMotion 只给演示用。
const running = new WeakMap(); // 框 → 正在播的这一次

/**
 * @param {Element} container
 * @param {() => void} change
 * @param {{ duration?: number, easing?: string, ignoreReducedMotion?: boolean }} [options]
 */
export function layoutFlip(container, change, {
  duration = 500,
  easing = 'cubic-bezier(0.2, 0.7, 0.3, 1)',
  ignoreReducedMotion = false,
} = {}) {
  const reduce = !ignoreReducedMotion && matchMedia('(prefers-reduced-motion: reduce)').matches;
  const shown = (el) => getComputedStyle(el).display !== 'none';
  const prev = running.get(container);

  // First:每一项现在在画面上的框(正在滑的算上此刻的位移)和排版时的大小;上一次正在淡出的项不算,它本来就要走
  const first = new Map();
  for (const el of container.children) {
    if (prev?.leaving.has(el) || !shown(el)) continue;
    first.set(el, { box: el.getBoundingClientRect(), display: getComputedStyle(el).display, width: el.offsetWidth, height: el.offsetHeight });
  }
  prev?.stop();
  change();
  if (reduce) return Promise.resolve();

  // 筛掉的项:临时显示出来、不占位置(absolute),不能点、不能选中,量完新布局再用位移挪回原来的地方,在那里淡出
  const leaving = new Map();
  for (const [el, was] of first) {
    if (el.parentElement !== container || shown(el)) continue;
    leaving.set(el, { style: el.getAttribute('style'), inert: el.inert });
    // display 加 important:不少网站的 CSS 写了 [hidden] { display: none !important }(Bootstrap、Tailwind 4),不加就显示不出来
    el.style.setProperty('display', was.display, 'important');
    Object.assign(el.style, { position: 'absolute', margin: '0', boxSizing: 'border-box', width: `${was.width}px`, height: `${was.height}px`, pointerEvents: 'none' });
    el.inert = true;
  }
  // Last:新布局里每一项的框
  const last = new Map();
  for (const el of container.children) if (!leaving.has(el) && shown(el)) last.set(el, el.getBoundingClientRect());

  // Invert + Play:位移、缩放叠加在项原来的值上(composite: 'add');透明度单独一个动画
  const timing = { duration, easing };
  const move = (el, frames, fill = 'none') => el.animate(frames, { ...timing, fill, composite: 'add' });
  const fade = (el, from, to, fill = 'none') => el.animate([{ opacity: from }, { opacity: to }], { ...timing, fill });
  const anims = [];
  for (const [el, now] of last) {
    const was = first.get(el)?.box;
    if (!was) {
      anims.push(move(el, [{ scale: 0.9 }, { scale: 1 }]), fade(el, 0, 1));
      continue;
    }
    const [dx, dy] = [was.left - now.left, was.top - now.top];
    if (Math.abs(dx) < 0.5 && Math.abs(dy) < 0.5) continue;
    anims.push(move(el, [{ translate: `${dx}px ${dy}px` }, { translate: '0px 0px' }]));
  }
  for (const el of leaving.keys()) {
    const was = first.get(el).box;
    const now = el.getBoundingClientRect();
    const at = `${was.left - now.left}px ${was.top - now.top}px`;
    anims.push(move(el, [{ translate: at, scale: 1 }, { translate: at, scale: 0.9 }], 'forwards'), fade(el, 1, 0, 'forwards'));
  }

  // 收尾:动画停掉,筛掉的项还原样式(真的藏起来)。播完、被下一次打断都走这里
  const run = {
    leaving,
    stop() {
      anims.forEach((a) => a.cancel());
      leaving.forEach(({ style, inert }, el) => {
        if (style === null) el.removeAttribute('style');
        else el.setAttribute('style', style);
        el.inert = inert;
      });
      leaving.clear();
      if (running.get(container) === run) running.delete(container);
    },
  };
  running.set(container, run);
  return Promise.all(anims.map((a) => a.finished.catch(() => undefined))).then(() => {
    if (running.get(container) === run) run.stop();
  });
}
HTML
<!-- 滑动重排:点筛选按钮,改布局的代码放进 layoutFlip;剩下的作品从原来的位置滑到新位置,筛掉的在原地淡出 -->
<div class="filters" role="group" aria-label="按类别筛选">
  <button type="button" data-tag="" aria-pressed="true">全部</button>
  <button type="button" data-tag="设计" aria-pressed="false">设计</button>
  <button type="button" data-tag="开发" aria-pressed="false">开发</button>
</div>
<ul class="works" style="display: grid; grid-template-columns: repeat(3, 1fr); gap: 16px; padding: 0; list-style: none">
  <li data-tag="开发"><a href="/works/site/">个人网站</a></li>
  <li data-tag="设计"><a href="/works/poster/">海报</a></li>
  <li data-tag="开发"><a href="/works/app/">小程序</a></li>
  <li data-tag="设计"><a href="/works/drawing/">插画</a></li>
</ul>
<script type="module">
  import { layoutFlip } from './layout-flip.js';
  const grid = document.querySelector('.works');
  const buttons = document.querySelectorAll('.filters button');
  buttons.forEach((button) => button.addEventListener('click', () => {
    const tag = button.dataset.tag;
    buttons.forEach((b) => b.setAttribute('aria-pressed', String(b === button)));
    layoutFlip(grid, () => {
      for (const item of grid.children) item.hidden = Boolean(tag) && item.dataset.tag !== tag;
    });
  }));
</script>

同属「页面与布局」的其他动效

查看全部 86 个动效 →

本次更新仅弹窗提醒一次 · 可随时在历史公告回看