中文
北煜ii · 个人网站把想法做成
能用的东西
Background & decoration · CSS only

Floating shapes

漂浮小图形

A few small decorative shapes around the content gently float up and down.

Also searched as: 几个小图形在空白处上下浮动, 四周漂着的小圆圈和方块, 装饰用的几何小图形浮起落下, 背景里轻轻上下晃的小点点

AI Markdown (.md)
Placement
Background, Hero heading
Mood
Soft, Playful
Trigger
一直循环
Site types
个人网站, 作品集, 工具站

How it works & accessibility

Five absolutely positioned shapes in an aria-hidden, click-through layer each loop their translate from 0 to -distance and back with ease-in-out keyframes, each one starting a fifth of a loop after the one before; only translate moves, the layer sits behind the content in an isolated stacking context and the shapes stay still with reduced motion.

Best for: Background, Hero heading (一直循环).

Reduced motion: 不动,小图形静止在原处

Mobile & touch: 和桌面一样;只动位移,手机上也不卡;屏幕窄时小图形可能压到文字,可以少放几个

Key parameters

ParameterKeyDefaultRangeNotes
一圈多久duration6000ms——
浮多高distance12px——

Ready-to-paste AI prompt

Add “Floating shapes” to the background in my project: Five absolutely positioned shapes in an aria-hidden, click-through layer each loop their translate from 0 to -distance and back with ease-in-out keyframes, each one starting a fifth of a loop after the one before; only translate moves, the layer sits behind the content in an isolated stacking context and the shapes stay still with reduced motion.
Parameters: duration: 6000ms, distance: 12px.
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/floating-shapes.md (canonical specification and reference code).

Dependency-free reference code

CSS
/* 漂浮小图形(Floating shapes)· 只用 CSS,不需要脚本。内容四周几个装饰小图形,各自缓慢地上下浮动。
   同类效果见 Animation Handbook 的 floating-elements、Motion Lexicon 的 idle-animation(都是 MIT);本站按通用做法自己实现,没有复制代码。
   用法:区块加 class="fs",最前面放 <span class="fs-shapes" aria-hidden="true"><i></i><i></i><i></i><i></i><i></i></span>,内容照常写在后面。
   - 装饰层铺满区块、在内容下面(区块是一个独立的层叠上下文),不挡点击,读屏不念(aria-hidden),伸出区块的部分裁掉。
   - 每个小图形一圈里 50% 时浮到最高(--fs-distance)、100% 落回原处,每一段都是 ease-in-out;
     第 2 到第 5 个比前一个晚五分之一圈开始,看起来此起彼伏。只动 translate 属性,交给显卡。
   - 区块要有定位:默认的 position: relative 写在 :where() 里,网站自己写的定位优先;别写 position: static。
   - 颜色:--fs-c1、--fs-c2;一圈多久:--fs-duration(默认 6 秒);浮多高:--fs-distance(默认 12px)。
   - 五个图形的位置、大小、形状写在下面的 :where() 里,网站可以按自己的版面改(比如屏幕窄时把压到文字的那个藏起来)。
   - 开启"减少动态"时:不动,小图形静止在原处。 */
/* 默认值写在 :where() 里(不占优先级):网站自己在 .fs 上改的变量、图形的位置,都用网站自己的 */
:where(.fs) {
  --fs-duration: 6000ms;
  --fs-distance: 12px;
  --fs-c1: #ffb6ca;
  --fs-c2: #a8c0ff;
  position: relative;
}
.fs {
  isolation: isolate;
}
.fs-shapes {
  position: absolute;
  inset: 0;
  z-index: -1;
  overflow: hidden;
  pointer-events: none;
}
.fs-shapes > i {
  position: absolute;
  animation: fs-float var(--fs-duration) ease-in-out infinite;
}
.fs-shapes > i:nth-child(2) { animation-delay: calc(var(--fs-duration) * 0.2); }
.fs-shapes > i:nth-child(3) { animation-delay: calc(var(--fs-duration) * 0.4); }
.fs-shapes > i:nth-child(4) { animation-delay: calc(var(--fs-duration) * 0.6); }
.fs-shapes > i:nth-child(5) { animation-delay: calc(var(--fs-duration) * 0.8); }
/* 五个图形:实心圆、空心圆、圆角方块、实心圆、小圆点,散在四周 */
:where(.fs-shapes > i:nth-child(1)) { left: 8%; top: 20%; width: 14px; height: 14px; border-radius: 50%; background: var(--fs-c1); }
:where(.fs-shapes > i:nth-child(2)) { left: 84%; top: 16%; width: 22px; height: 22px; border-radius: 50%; border: 3px solid var(--fs-c2); box-sizing: border-box; }
:where(.fs-shapes > i:nth-child(3)) { left: 16%; top: 74%; width: 14px; height: 14px; border-radius: 3px; rotate: 18deg; background: var(--fs-c2); }
:where(.fs-shapes > i:nth-child(4)) { left: 76%; top: 70%; width: 16px; height: 16px; border-radius: 50%; background: var(--fs-c1); }
:where(.fs-shapes > i:nth-child(5)) { left: 52%; top: 12%; width: 8px; height: 8px; border-radius: 50%; background: var(--fs-c2); }
@keyframes fs-float {
  0%, 100% { translate: 0 0; }
  50% { translate: 0 calc(var(--fs-distance) * -1); }
}
@media (prefers-reduced-motion: reduce) {
  .fs-shapes > i { animation: none; }
}
HTML
<!-- 漂浮小图形:区块加 class="fs",最前面放装饰层(五个小图形),内容照常写在后面 -->
<section class="fs">
  <span class="fs-shapes" aria-hidden="true"><i></i><i></i><i></i><i></i><i></i></span>
  <h1>你好,我是北煜ii</h1>
  <p>独立设计师,做网站和小工具</p>
</section>

More in Background & decoration

Browse all 86 effects →

Shown once per release · Always available in changelog