翻炒动画

五月 24, 2024 / Mr.x / 1阅读 / 0评论/ 分类: 创意
翻炒动画 — 荣小站

翻炒动画

使用 SVG 和 CSS 实现的锅铲翻炒动画效果,模拟烹饪时翻动食材的动态场景。

▼ 效果展示

这是一个精美的翻炒动画效果,使用 SVG 矢量图形和 CSS 动画实现。动画模拟了锅铲翻动食材的过程,包括锅铲的旋转摆动、油滴飞溅、以及阴影的动态变化。整个效果完全使用 CSS 动画,无需 JavaScript。

核心特性

  • 纯 SVG + CSS:无需 JavaScript,完全使用 SVG 和 CSS 动画
  • 锅铲翻转:模拟真实翻炒动作的旋转摆动
  • 油滴飞溅:多个油滴根据时序飞溅
  • 阴影效果:锅铲阴影随动作变化
  • 暗色主题:支持根据系统主题自动切换

完整代码

以下是完整的代码,分为 HTML、CSS 两部分,点击各部分查看:

📄 HTML 结构

<svg class="pl" viewBox="0 0 128 128" width="128px" height="128px" role="img" aria-label="A pan being used to flip a blob resembling bacon as it splashes drops of grease in and out"> <clipPath id="pan-clip"> <rect rx="12" ry="14" x="4" y="52" width="68" height="28" /> </clipPath> <defs> <linearGradient id="pl-grad" x1="0" y1="0" x2="0" y2="1"> <stop offset="0%" stop-color="#000" /> <stop offset="100%" stop-color="#fff" /> </linearGradient> <mask id="pl-mask"> <rect x="0" y="0" width="88" height="80" fill="url(#pl-grad)" /> </mask> </defs> <g fill="currentColor"> <g fill="none" stroke-dasharray="20 221" stroke-dashoffset="20" stroke-linecap="round" stroke-width="4"> <g stroke="hsl(38,90%,50%)"> <circle class="pl__ring" cx="44" cy="40" r="35" transform="rotate(90,44,40)" /> </g> <g stroke="hsl(8,90%,40%)" mask="url(#pl-mask)"> <circle class="pl__ring" cx="44" cy="40" r="35" transform="rotate(90,44,40)" /> </g> </g> <g fill="hsla(223,10%,70%,0)"> <g class="pl__drop pl__drop--1"> <circle class="pl__drop-inner" cx="13" cy="60" r="2" /> </g> <g class="pl__drop pl__drop--2"> <circle class="pl__drop-inner" cx="13" cy="60" r="2" /> </g> <g class="pl__drop pl__drop--3"> <circle class="pl__drop-inner" cx="67" cy="72" r="2" /> </g> <g class="pl__drop pl__drop--4"> <circle class="pl__drop-inner" cx="67" cy="72" r="2" /> </g> <g class="pl__drop pl__drop--5"> <circle class="pl__drop-inner" cx="67" cy="72" r="2" /> </g> </g> <g class="pl__pan"> <rect rx="2" ry="2" x="4" y="66" width="68" height="14" clip-path="url(#pan-clip)" id="pan" /> <rect rx="2" ry="2" x="76" y="66" width="48" height="7" /> </g> <rect class="pl__shadow" fill="hsla(223,10%,50%,0.2)" rx="3.5" ry="3.5" x="10" y="121" width="60" height="7" /> </g> </svg>

🎨 CSS 样式

* { border: 0; box-sizing: border-box; margin: 0; padding: 0; } :root { --hue: 223; --bg: hsl(var(--hue), 10%, 90%); --fg: hsl(var(--hue), 10%, 10%); --trans-dur: 0.3s; font-size: calc(14px + (30 - 14) * (100vw - 280px) / (3840 - 280)); } body { background-color: var(--bg); color: var(--fg); display: flex; font: 1em/1.5 sans-serif; height: 100vh; transition: background-color var(--trans-dur), color var(--trans-dur); } .pl { margin: auto; width: 12em; height: 12em; } .pl__drop, .pl__drop-inner, .pl__pan, .pl__ring, .pl__shadow { animation: pan 2s cubic-bezier(0.65, 0, 0.35, 1) infinite; } .pl__drop { transform-origin: 13px 60px; } .pl__drop-inner { animation-timing-function: cubic-bezier(0.33, 0.84, 0.67, 0.84); } .pl__drop--1 { animation-name: drop-1; } .pl__drop--1 .pl__drop-inner { animation-name: drop-1-inner; } .pl__drop--2 { animation-name: drop-2; } .pl__drop--2 .pl__drop-inner { animation-name: drop-2-inner; } .pl__drop--3 { transform-origin: 67px 72px; animation-name: drop-3; } .pl__drop--3 .pl__drop-inner { animation-name: drop-3-inner; } .pl__drop--4 { transform-origin: 67px 72px; animation-name: drop-4; } .pl__drop--4 .pl__drop-inner { animation-name: drop-4-inner; } .pl__drop--5 { transform-origin: 67px 72px; animation-name: drop-5; } .pl__drop--5 .pl__drop-inner { animation-name: drop-5-inner; } .pl__pan { transform-origin: 36px 74px; } .pl__ring { animation-name: flip-ring; } .pl__shadow { animation-name: pan-shadow; transform-origin: 36px 124.5px; } @media (prefers-color-scheme: dark) { :root { --bg: hsl(var(--hue), 10%, 10%); --fg: hsl(var(--hue), 10%, 90%); } } @keyframes drop-1 { from { animation-timing-function: steps(1, end); transform: translate(0, 0); visibility: hidden; } 30% { animation-timing-function: linear; transform: translate(0, 0); visibility: visible; } 50%, to { transform: translate(-6px, 0); } } @keyframes drop-1-inner { from, 30% { fill: hsl(223, 10%, 70%, 1); transform: translate(0, 0); } 50%, to { fill: hsl(223, 10%, 70%, 0); transform: translate(0, -27px); } } /* 更多关键帧动画... */ @keyframes flip-ring { from, 27% { animation-timing-function: cubic-bezier(0.33, 0.84, 0.67, 0.84); stroke-dashoffset: 20; stroke-width: 4px; } 53.5% { animation-timing-function: cubic-bezier(0.33, 0.16, 0.67, 0.16); stroke-dashoffset: -100; stroke-width: 10px; } 80%, to { stroke-dashoffset: -220; stroke-width: 4px; } } @keyframes pan { from, 88%, to { transform: translate(0, 0) rotate(0); } 20% { animation-timing-function: cubic-bezier(0.33, 0.16, 0.67, 0.16); transform: translate(-5px, 0) rotate(-30deg); } 30% { animation-timing-function: cubic-bezier(0.33, 0.84, 0.67, 0.84); transform: translate(0, 0) rotate(20deg); } 60%, 78% { animation-timing-function: linear; transform: translate(0, 0) rotate(0); } 81.33% { animation-timing-function: linear; transform: translate(0, 4px) rotate(0); } 84.67% { animation-timing-function: linear; transform: translate(0, -2px) rotate(0); } }

技术要点

技术点说明
SVG 动画使用 SVG 矢量图形创建锅铲和食材
CSS 动画使用 @keyframes 定义复杂动画序列
cubic-bezier自定义缓动函数模拟真实运动
transform-origin设置旋转中心点
stroke-dasharray创建环形进度效果
prefers-color-scheme检测系统主题自动切换

使用说明

  1. 将 HTML 结构复制到你的页面中
  2. 添加对应的 CSS 样式
  3. 动画会自动播放
  4. 可根据需要调整尺寸和颜色

#翻炒动画(1)

评论