{"id":207,"date":"2026-07-16T09:43:49","date_gmt":"2026-07-16T01:43:49","guid":{"rendered":"https:\/\/yykcj.com\/?p=207"},"modified":"2026-07-16T09:43:49","modified_gmt":"2026-07-16T01:43:49","slug":"css-%e5%8a%a8%e7%94%bb%e4%b8%8e%e4%ba%a4%e4%ba%92%e8%ae%be%e8%ae%a1%e8%bf%9b%e9%98%b6%ef%bc%9a%e6%89%93%e9%80%a0%e9%ab%98%e6%80%a7%e8%83%bd%e3%80%81%e5%8f%af%e5%a4%8d%e7%94%a8%e7%9a%84%e5%8a%a8","status":"publish","type":"post","link":"https:\/\/yykcj.com\/?p=207","title":{"rendered":"CSS \u52a8\u753b\u4e0e\u4ea4\u4e92\u8bbe\u8ba1\u8fdb\u9636\uff1a\u6253\u9020\u9ad8\u6027\u80fd\u3001\u53ef\u590d\u7528\u7684\u52a8\u6548\u7cfb\u7edf"},"content":{"rendered":"<h2>\u5f15\u8a00<\/h2>\n<p>\u5728\u73b0\u4ee3 Web \u5f00\u53d1\u4e2d\uff0cCSS \u52a8\u753b\u5df2\u4ece\u7b80\u5355\u7684\u88c5\u9970\u6f14\u53d8\u4e3a\u63d0\u5347\u7528\u6237\u4f53\u9a8c\u7684\u6838\u5fc3\u624b\u6bb5\u3002\u7136\u800c\uff0c\u8bb8\u591a\u5f00\u53d1\u8005\u4ecd\u505c\u7559\u5728 <code>transition<\/code> \u548c <code>keyframes<\/code> \u7684\u57fa\u7840\u4f7f\u7528\u4e0a\uff0c\u9762\u5bf9\u590d\u6742\u52a8\u6548\u65f6\u5f80\u5f80\u9047\u5230\u6027\u80fd\u74f6\u9888\u6216\u4ee3\u7801\u6df7\u4e71\u3002\u672c\u6587\u5c06\u5e26\u4f60\u638c\u63e1 CSS \u52a8\u753b\u7684\u8fdb\u9636\u6280\u5de7\uff0c\u5305\u62ec Web Animations API\u3001\u786c\u4ef6\u52a0\u901f\u3001\u52a8\u753b\u7f16\u6392\u4e0e\u54cd\u5e94\u5f0f\u52a8\u6548\uff0c\u5e76\u901a\u8fc7\u4e00\u4e2a\u5b8c\u6574\u7684\u5b9e\u6218\u6848\u4f8b\u6784\u5efa\u53ef\u590d\u7528\u7684\u52a8\u6548\u7cfb\u7edf\u3002<\/p>\n<h2>1. \u4ece CSS \u5230 Web Animations API<\/h2>\n<h3>1.1 CSS \u52a8\u753b\u7684\u5c40\u9650<\/h3>\n<p>\u4f20\u7edf\u7684 CSS \u52a8\u753b\u867d\u7136\u7b80\u5355\uff0c\u4f46\u5728\u52a8\u6001\u63a7\u5236\u3001\u590d\u6742\u5e8f\u5217\u548c\u4ea4\u4e92\u54cd\u5e94\u65b9\u9762\u529b\u4e0d\u4ece\u5fc3\u3002\u4f8b\u5982\uff0c\u6682\u505c\u3001\u53cd\u8f6c\u3001\u52a8\u6001\u4fee\u6539\u53c2\u6570\u7b49\u64cd\u4f5c\u9700\u8981\u501f\u52a9 JavaScript \u548c\u7c7b\u540d\u5207\u6362\uff0c\u5bb9\u6613\u9020\u6210\u4ee3\u7801\u8026\u5408\u3002<\/p>\n<h3>1.2 Web Animations API \u7b80\u4ecb<\/h3>\n<p>Web Animations API \u63d0\u4f9b\u4e86 JavaScript \u76f4\u63a5\u63a7\u5236\u52a8\u753b\u7684\u80fd\u529b\uff0c\u8bed\u6cd5\u4e0e CSS \u7c7b\u4f3c\uff0c\u4f46\u66f4\u7075\u6d3b\u3002\u57fa\u672c\u7528\u6cd5\u5982\u4e0b\uff1a<\/p>\n<pre><code class=\"language-javascript\">\nconst element = document.querySelector(&#x27;.box&#x27;);\nconst animation = element.animate([\n  { transform: &#x27;translateX(0)&#x27;, opacity: 1 },\n  { transform: &#x27;translateX(200px)&#x27;, opacity: 0.5 }\n], {\n  duration: 1000,\n  easing: &#x27;ease-in-out&#x27;,\n  fill: &#x27;forwards&#x27;\n});\n<\/code><\/pre>\n<p><code>animate()<\/code> \u65b9\u6cd5\u8fd4\u56de\u4e00\u4e2a <code>Animation<\/code> \u5bf9\u8c61\uff0c\u652f\u6301 <code>play()<\/code>\u3001<code>pause()<\/code>\u3001<code>reverse()<\/code>\u3001<code>finish()<\/code> \u7b49\u65b9\u6cd5\uff0c\u4ee5\u53ca <code>onfinish<\/code> \u4e8b\u4ef6\u3002<\/p>\n<h3>1.3 \u5b9e\u6218\uff1a\u53ef\u63a7\u5236\u7684\u8fdb\u5ea6\u6761\u52a8\u753b<\/h3>\n<p>\u521b\u5efa\u4e00\u4e2a\u8fdb\u5ea6\u6761\uff0c\u901a\u8fc7\u6ed1\u5757\u63a7\u5236\u52a8\u753b\u8fdb\u5ea6\uff1a<\/p>\n<pre><code class=\"language-html\">\n&lt;div id=&quot;progress-bar&quot; style=&quot;width:0; height:20px; background:blue;&quot;&gt;&lt;\/div&gt;\n&lt;input type=&quot;range&quot; id=&quot;slider&quot; min=&quot;0&quot; max=&quot;100&quot; value=&quot;0&quot;&gt;\n<\/code><\/pre>\n<pre><code class=\"language-javascript\">\nconst bar = document.getElementById(&#x27;progress-bar&#x27;);\nconst slider = document.getElementById(&#x27;slider&#x27;);\n\nconst animation = bar.animate([\n  { width: &#x27;0%&#x27; },\n  { width: &#x27;100%&#x27; }\n], {\n  duration: 2000,\n  fill: &#x27;forwards&#x27;\n});\n\nanimation.pause(); \/\/ \u521d\u59cb\u6682\u505c\n\nslider.addEventListener(&#x27;input&#x27;, () =&gt; {\n  animation.currentTime = (slider.value \/ 100) * animation.effect.getTiming().duration;\n});\n<\/code><\/pre>\n<p>\ud83d\udca1 <strong>\u6ce8\u610f<\/strong>\uff1a<code>fill: 'forwards'<\/code> \u786e\u4fdd\u52a8\u753b\u7ed3\u675f\u540e\u4fdd\u6301\u6700\u7ec8\u72b6\u6001\uff0c\u5426\u5219\u5143\u7d20\u4f1a\u56de\u9000\u5230\u521d\u59cb\u72b6\u6001\u3002<\/p>\n<h2>2. \u786c\u4ef6\u52a0\u901f\u4e0e\u6027\u80fd\u4f18\u5316<\/h2>\n<h3>2.1 \u89e6\u53d1\u786c\u4ef6\u52a0\u901f\u7684\u5c5e\u6027<\/h3>\n<p>\u73b0\u4ee3\u6d4f\u89c8\u5668\u5229\u7528 GPU \u52a0\u901f\u67d0\u4e9b CSS \u5c5e\u6027\u7684\u6e32\u67d3\uff0c\u4e3b\u8981\u6d89\u53ca <code>transform<\/code> \u548c <code>opacity<\/code>\u3002\u907f\u514d\u52a8\u753b <code>left<\/code>\u3001<code>top<\/code>\u3001<code>width<\/code>\u3001<code>height<\/code> \u7b49\u5c5e\u6027\uff0c\u5b83\u4eec\u4f1a\u89e6\u53d1\u91cd\u6392\uff08reflow\uff09\u3002<\/p>\n<p><strong>\u63a8\u8350\u52a8\u753b\u5c5e\u6027<\/strong>\uff1a<code>transform<\/code>\uff08translate\u3001scale\u3001rotate\uff09\u3001<code>opacity<\/code>\u3001<code>filter<\/code>\uff08\u90e8\u5206\uff09\u3002<\/p>\n<h3>2.2 \u4f7f\u7528 will-change \u63d0\u793a<\/h3>\n<p><code>will-change<\/code> \u5c5e\u6027\u544a\u77e5\u6d4f\u89c8\u5668\u63d0\u524d\u4f18\u5316\uff1a<\/p>\n<pre><code class=\"language-css\">\n.animated-element {\n  will-change: transform, opacity;\n}\n<\/code><\/pre>\n<p>\u4f46\u4e0d\u8981\u6ee5\u7528\uff0c\u8fc7\u591a\u5143\u7d20\u8bbe\u7f6e <code>will-change<\/code> \u4f1a\u6d88\u8017\u5185\u5b58\u3002<\/p>\n<h3>2.3 \u5b9e\u6218\uff1a\u6eda\u52a8\u9a71\u52a8\u7684\u89c6\u5dee\u6548\u679c<\/h3>\n<p>\u5229\u7528 <code>transform: translateZ()<\/code> \u548c <code>perspective<\/code> \u521b\u5efa 3D \u89c6\u5dee\uff0c\u6027\u80fd\u4f18\u4e8e JavaScript \u76d1\u542c\u6eda\u52a8\uff1a<\/p>\n<pre><code class=\"language-css\">\n.parallax-container {\n  perspective: 1px;\n  height: 100vh;\n  overflow-x: hidden;\n  overflow-y: auto;\n}\n.parallax-layer {\n  position: absolute;\n  top: 0;\n  left: 0;\n  right: 0;\n  bottom: 0;\n  transform: translateZ(-1px) scale(2);\n}\n<\/code><\/pre>\n<p>\u6ce8\u610f\uff1a<code>scale(2)<\/code> \u7528\u4e8e\u8865\u507f <code>translateZ(-1px)<\/code> \u9020\u6210\u7684\u7f29\u5c0f\u6548\u679c\uff0c\u4fdd\u6301\u5927\u5c0f\u4e0d\u53d8\u3002<\/p>\n<h2>3. \u52a8\u753b\u7f16\u6392\uff1a\u6253\u9020\u6d41\u7545\u7684\u5e8f\u5217\u52a8\u753b<\/h2>\n<h3>3.1 \u4f7f\u7528 animation-delay \u7684\u5c40\u9650<\/h3>\n<p>CSS \u4e2d\u901a\u8fc7 <code>animation-delay<\/code> \u5b9e\u73b0\u5e8f\u5217\uff0c\u4f46\u96be\u4ee5\u52a8\u6001\u8c03\u6574\u3002\u63a8\u8350\u4f7f\u7528 Web Animations API \u7684 <code>delay<\/code> \u548c <code>endDelay<\/code>\u3002<\/p>\n<h3>3.2 \u5b9e\u6218\uff1a\u5217\u8868\u9010\u9879\u5165\u573a\u52a8\u753b<\/h3>\n<pre><code class=\"language-javascript\">\nconst items = document.querySelectorAll(&#x27;.item&#x27;);\n\nitems.forEach((item, index) =&gt; {\n  const anim = item.animate([\n    { opacity: 0, transform: &#x27;translateY(20px)&#x27; },\n    { opacity: 1, transform: &#x27;translateY(0)&#x27; }\n  ], {\n    duration: 500,\n    delay: index * 100,\n    fill: &#x27;backwards&#x27;\n  });\n});\n<\/code><\/pre>\n<p><code>fill: 'backwards'<\/code> \u786e\u4fdd\u5143\u7d20\u5728\u5ef6\u8fdf\u671f\u95f4\u4fdd\u6301\u521d\u59cb\u72b6\u6001\uff08\u900f\u660e\u3001\u4e0a\u79fb\uff09\uff0c\u907f\u514d\u7a81\u7136\u51fa\u73b0\u3002<\/p>\n<h3>3.3 \u4f7f\u7528 Promise \u63a7\u5236\u5e8f\u5217<\/h3>\n<p>Web Animations API \u7684 <code>animation.finished<\/code> \u8fd4\u56de Promise\uff0c\u4fbf\u4e8e\u7f16\u6392\uff1a<\/p>\n<pre><code class=\"language-javascript\">\nasync function runSequence() {\n  await item1.animate([...], { duration: 300 }).finished;\n  await item2.animate([...], { duration: 300 }).finished;\n  \/\/ ...\n}\n<\/code><\/pre>\n<h2>4. \u54cd\u5e94\u5f0f\u52a8\u6548\uff1a\u9002\u914d\u4e0d\u540c\u5c4f\u5e55\u548c\u8bbe\u5907<\/h2>\n<h3>4.1 \u4f7f\u7528\u5a92\u4f53\u67e5\u8be2\u63a7\u5236\u52a8\u753b<\/h3>\n<pre><code class=\"language-css\">\n@media (prefers-reduced-motion: reduce) {\n  * {\n    animation-duration: 0.01ms !important;\n    transition-duration: 0.01ms !important;\n  }\n}\n<\/code><\/pre>\n<p>\u5c0a\u91cd\u7528\u6237\u5bf9\u51cf\u5c11\u52a8\u6548\u7684\u504f\u597d\uff0c\u63d0\u5347\u53ef\u8bbf\u95ee\u6027\u3002<\/p>\n<h3>4.2 \u57fa\u4e8e\u89c6\u53e3\u5927\u5c0f\u7684\u52a8\u753b\u53c2\u6570<\/h3>\n<p>\u4f7f\u7528 CSS \u81ea\u5b9a\u4e49\u5c5e\u6027\u548c <code>calc()<\/code>\uff1a<\/p>\n<pre><code class=\"language-css\">\n:root {\n  --move-distance: 100px;\n}\n@media (min-width: 768px) {\n  :root {\n    --move-distance: 200px;\n  }\n}\n.element {\n  animation: slide 1s forwards;\n}\n@keyframes slide {\n  to { transform: translateX(var(--move-distance)); }\n}\n<\/code><\/pre>\n<h3>4.3 \u5b9e\u6218\uff1a\u81ea\u9002\u5e94\u89e6\u6478\u53cd\u9988<\/h3>\n<p>\u79fb\u52a8\u7aef\u70b9\u51fb\u53cd\u9988\u4f7f\u7528 <code>:active<\/code> \u914d\u5408 <code>transform: scale()<\/code>\uff0c\u4f46\u9700\u8003\u8651\u89e6\u5c4f\u4e0e\u9f20\u6807\u7684\u5dee\u5f02\uff1a<\/p>\n<pre><code class=\"language-css\">\n.button {\n  transition: transform 0.15s;\n}\n.button:active {\n  transform: scale(0.95);\n}\n@media (hover: none) {\n  .button:active {\n    transform: scale(0.9);\n  }\n}\n<\/code><\/pre>\n<h2>5. \u6784\u5efa\u53ef\u590d\u7528\u7684\u52a8\u6548\u7cfb\u7edf<\/h2>\n<h3>5.1 \u8bbe\u8ba1\u539f\u5219<\/h3>\n<ul>\n<li><strong>\u539f\u5b50\u5316<\/strong>\uff1a\u5c06\u52a8\u6548\u62c6\u5206\u4e3a\u57fa\u7840\u5355\u5143\uff08\u5982\u6de1\u5165\u3001\u6ed1\u52a8\uff09<\/li>\n<li><strong>\u53ef\u914d\u7f6e<\/strong>\uff1a\u901a\u8fc7 CSS \u81ea\u5b9a\u4e49\u5c5e\u6027\u63a7\u5236\u53c2\u6570<\/li>\n<li><strong>\u53ef\u7ec4\u5408<\/strong>\uff1a\u4f7f\u7528 JavaScript \u7ec4\u5408\u591a\u4e2a\u52a8\u753b<\/li>\n<\/ul>\n<h3>5.2 \u5b9e\u6218\uff1a\u52a8\u6548\u5e93\u6838\u5fc3\u5b9e\u73b0<\/h3>\n<pre><code class=\"language-javascript\">\n\/\/ animations.js\nexport const fadeIn = (el, duration = 300, delay = 0) =&gt; {\n  return el.animate([\n    { opacity: 0 },\n    { opacity: 1 }\n  ], { duration, delay, fill: &#x27;backwards&#x27; });\n};\n\nexport const slideIn = (el, direction = &#x27;left&#x27;, duration = 300) =&gt; {\n  const offset = direction === &#x27;left&#x27; ? &#x27;-100px&#x27; : &#x27;100px&#x27;;\n  return el.animate([\n    { transform: `translateX(${offset})`, opacity: 0 },\n    { transform: &#x27;translateX(0)&#x27;, opacity: 1 }\n  ], { duration, fill: &#x27;backwards&#x27; });\n};\n\n\/\/ \u7ec4\u5408\u4f7f\u7528\nasync function entranceAnimation(el) {\n  await fadeIn(el, 200).finished;\n  await slideIn(el, &#x27;right&#x27;, 300).finished;\n}\n<\/code><\/pre>\n<h2>6. \u5e38\u89c1\u5751\u4e0e\u6700\u4f73\u5b9e\u8df5<\/h2>\n<h3>6.1 \u52a8\u753b\u95ea\u70c1\u95ee\u9898<\/h3>\n<p>\u539f\u56e0\uff1a<code>fill<\/code> \u5c5e\u6027\u672a\u8bbe\u7f6e\u6216\u9519\u8bef\u3002\u786e\u4fdd\u4f7f\u7528 <code>fill: 'backwards'<\/code> \u6216 <code>fill: 'forwards'<\/code> \u6839\u636e\u9700\u6c42\u3002<\/p>\n<h3>6.2 \u4e0e\u6846\u67b6\u96c6\u6210<\/h3>\n<p>\u5728 React \u4e2d\uff0c\u4f7f\u7528 <code>useRef<\/code> \u548c <code>useEffect<\/code> \u7ba1\u7406\u52a8\u753b\u751f\u547d\u5468\u671f\uff0c\u907f\u514d\u91cd\u590d\u521b\u5efa\uff1a<\/p>\n<pre><code class=\"language-jsx\">\nfunction AnimatedBox() {\n  const ref = useRef(null);\n  useEffect(() =&gt; {\n    const anim = ref.current.animate([...], { duration: 500 });\n    return () =&gt; anim.cancel();\n  }, []);\n  return &lt;div ref={ref} \/&gt;;\n}\n<\/code><\/pre>\n<h3>6.3 \u6027\u80fd\u76d1\u63a7<\/h3>\n<p>\u4f7f\u7528 Chrome DevTools \u7684 Performance \u9762\u677f\u68c0\u67e5\u52a8\u753b\u5e27\u7387\uff08FPS\uff09\uff0c\u786e\u4fdd\u4fdd\u6301\u5728 60fps\u3002<\/p>\n<h2>\u603b\u7ed3<\/h2>\n<p>\u672c\u6587\u4ece Web Animations API\u3001\u786c\u4ef6\u52a0\u901f\u3001\u52a8\u753b\u7f16\u6392\u3001\u54cd\u5e94\u5f0f\u52a8\u6548\u5230\u6784\u5efa\u52a8\u6548\u7cfb\u7edf\uff0c\u5168\u9762\u8986\u76d6\u4e86 CSS \u52a8\u753b\u8fdb\u9636\u77e5\u8bc6\u3002\u5173\u952e\u5728\u4e8e\uff1a<\/p>\n<ol>\n<li>\u4f18\u5148\u4f7f\u7528 <code>transform<\/code> \u548c <code>opacity<\/code> \u52a8\u753b\uff0c\u5229\u7528 GPU \u52a0\u901f\u3002<\/li>\n<li>\u4f7f\u7528 Web Animations API \u5b9e\u73b0\u590d\u6742\u63a7\u5236\u548c\u5e8f\u5217\u3002<\/li>\n<li>\u8003\u8651\u53ef\u8bbf\u95ee\u6027\u4e0e\u54cd\u5e94\u5f0f\u8bbe\u8ba1\u3002<\/li>\n<li>\u5c01\u88c5\u53ef\u590d\u7528\u7684\u52a8\u6548\u6a21\u5757\uff0c\u63d0\u5347\u5f00\u53d1\u6548\u7387\u3002<\/li>\n<\/ol>\n<p>\u4e0b\u4e00\u6b65\uff0c\u4f60\u53ef\u4ee5\u63a2\u7d22 CSS Houdini \u7684 <code>AnimationWorklet<\/code>\uff0c\u5b9e\u73b0\u66f4\u7cbe\u7ec6\u7684\u52a8\u753b\u63a7\u5236\u3002<\/p>\n<p>&#8212;<\/p>\n<p><em>\u672c\u6587\u6240\u6709\u4ee3\u7801\u793a\u4f8b\u5747\u53ef\u5728\u73b0\u4ee3\u6d4f\u89c8\u5668\uff08Chrome 80+\u3001Firefox 75+\uff09\u4e2d\u8fd0\u884c\u3002<\/em><\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u672c\u6587\u6df1\u5165\u63a2\u8ba8 CSS \u52a8\u753b\u7684\u9ad8\u9636\u6280\u5de7\uff0c\u5305\u62ec Web Animations API\u3001\u786c\u4ef6\u52a0\u901f\u3001\u52a8\u753b\u7f16\u6392\u4e0e\u54cd\u5e94\u5f0f\u52a8\u6548\u3002\u901a\u8fc7\u5b9e\u6218\u6848\u4f8b\uff0c\u6559\u4f60\u6784\u5efa\u53ef\u7ef4\u62a4\u7684\u52a8\u6548\u7cfb\u7edf\uff0c\u63d0\u5347\u4ea4\u4e92\u4f53\u9a8c\u3002<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[3],"tags":[137,138,36,90,6],"class_list":["post-207","post","type-post","status-publish","format-standard","hentry","category-3","tag-css","tag-web-animations-api","tag-36","tag-90","tag-6"],"_links":{"self":[{"href":"https:\/\/yykcj.com\/index.php?rest_route=\/wp\/v2\/posts\/207","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/yykcj.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/yykcj.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/yykcj.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/yykcj.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=207"}],"version-history":[{"count":0,"href":"https:\/\/yykcj.com\/index.php?rest_route=\/wp\/v2\/posts\/207\/revisions"}],"wp:attachment":[{"href":"https:\/\/yykcj.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=207"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/yykcj.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=207"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/yykcj.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=207"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}