{"id":298,"date":"2026-10-28T11:01:16","date_gmt":"2026-10-28T03:01:16","guid":{"rendered":"https:\/\/yykcj.com\/?p=298"},"modified":"2026-10-28T11:01:16","modified_gmt":"2026-10-28T03:01:16","slug":"%e6%b5%8f%e8%a7%88%e5%99%a8%e6%b8%b2%e6%9f%93%e5%8e%9f%e7%90%86%e4%b8%8e%e6%80%a7%e8%83%bd%e4%bc%98%e5%8c%96%e5%ae%9e%e6%88%98%e6%8c%87%e5%8d%97","status":"publish","type":"post","link":"https:\/\/yykcj.com\/?p=298","title":{"rendered":"\u6d4f\u89c8\u5668\u6e32\u67d3\u539f\u7406\u4e0e\u6027\u80fd\u4f18\u5316\u5b9e\u6218\u6307\u5357"},"content":{"rendered":"<h2>\u5f15\u8a00<\/h2>\n<p>\u4f5c\u4e3a\u524d\u7aef\u5f00\u53d1\u8005\uff0c\u6211\u4eec\u7ecf\u5e38\u9047\u5230\u9875\u9762\u5361\u987f\u3001\u52a8\u753b\u4e0d\u6d41\u7545\u7684\u95ee\u9898\u3002\u8981\u89e3\u51b3\u8fd9\u4e9b\u95ee\u9898\uff0c\u5fc5\u987b\u7406\u89e3\u6d4f\u89c8\u5668\u662f\u5982\u4f55\u5c06HTML\u3001CSS\u548cJavaScript\u8f6c\u6362\u6210\u5c4f\u5e55\u4e0a\u7684\u50cf\u7d20\u7684\u3002\u672c\u6587\u5c06\u5e26\u4f60\u6df1\u5165\u6d4f\u89c8\u5668\u6e32\u67d3\u539f\u7406\uff0c\u5e76\u901a\u8fc7\u5b9e\u9645\u4ee3\u7801\u6f14\u793a\u6027\u80fd\u4f18\u5316\u6280\u5de7\u3002<\/p>\n<h2>1. \u6d4f\u89c8\u5668\u6e32\u67d3\u6d41\u7a0b\u7b80\u8ff0<\/h2>\n<p>\u6d4f\u89c8\u5668\u6e32\u67d3\u4e3b\u8981\u5206\u4e3a\u4ee5\u4e0b\u6b65\u9aa4\uff1a<\/p>\n<ol>\n<li><strong>\u89e3\u6790HTML<\/strong>\uff1a\u6784\u5efaDOM\u6811\u3002<\/li>\n<li><strong>\u89e3\u6790CSS<\/strong>\uff1a\u6784\u5efaCSSOM\u6811\u3002<\/li>\n<li><strong>\u5408\u5e76<\/strong>\uff1a\u5c06DOM\u548cCSSOM\u5408\u5e76\u6210\u6e32\u67d3\u6811\uff08Render Tree\uff09\u3002<\/li>\n<li><strong>\u5e03\u5c40\uff08Layout\/Reflow\uff09<\/strong>\uff1a\u8ba1\u7b97\u6bcf\u4e2a\u8282\u70b9\u7684\u51e0\u4f55\u4fe1\u606f\u3002<\/li>\n<li><strong>\u7ed8\u5236\uff08Paint\uff09<\/strong>\uff1a\u586b\u5145\u50cf\u7d20\u3002<\/li>\n<li><strong>\u5408\u6210\uff08Composite\uff09<\/strong>\uff1a\u5c06\u56fe\u5c42\u5408\u5e76\u663e\u793a\u3002<\/li>\n<\/ol>\n<p>\u5f53JavaScript\u4fee\u6539DOM\u6216CSS\u65f6\uff0c\u53ef\u80fd\u89e6\u53d1<strong>\u91cd\u6392\uff08Reflow\uff09<\/strong>\u3001<strong>\u91cd\u7ed8\uff08Repaint\uff09<\/strong>\u6216\u4ec5<strong>\u5408\u6210<\/strong>\u3002\u7406\u89e3\u8fd9\u4e9b\u89e6\u53d1\u6761\u4ef6\u5bf9\u6027\u80fd\u4f18\u5316\u81f3\u5173\u91cd\u8981\u3002<\/p>\n<h2>2. \u91cd\u6392\u4e0e\u91cd\u7ed8\uff1a\u4ee3\u4ef7\u9ad8\u6602\u7684\u64cd\u4f5c<\/h2>\n<ul>\n<li><strong>\u91cd\u6392<\/strong>\uff1a\u5f53\u5143\u7d20\u7684\u51e0\u4f55\u5c5e\u6027\uff08\u5bbd\u9ad8\u3001\u4f4d\u7f6e\uff09\u6539\u53d8\u65f6\uff0c\u6d4f\u89c8\u5668\u9700\u8981\u91cd\u65b0\u8ba1\u7b97\u5e03\u5c40\u3002\u4ee3\u4ef7\u6700\u9ad8\u3002<\/li>\n<li><strong>\u91cd\u7ed8<\/strong>\uff1a\u5f53\u5143\u7d20\u7684\u5916\u89c2\uff08\u989c\u8272\u3001\u80cc\u666f\uff09\u6539\u53d8\uff0c\u4f46\u5e03\u5c40\u4e0d\u53d8\u65f6\uff0c\u6d4f\u89c8\u5668\u91cd\u65b0\u7ed8\u5236\u5143\u7d20\u3002\u4ee3\u4ef7\u6b21\u4e4b\u3002<\/li>\n<li><strong>\u5408\u6210<\/strong>\uff1a\u4ec5\u6539\u53d8<code>transform<\/code>\u6216<code>opacity<\/code>\u65f6\uff0c\u6d4f\u89c8\u5668\u53ef\u4ee5\u5728\u5408\u6210\u5c42\u4e0a\u5904\u7406\uff0c\u4e0d\u89e6\u53d1\u91cd\u6392\u548c\u91cd\u7ed8\uff0c\u6027\u80fd\u6700\u4f73\u3002<\/li>\n<\/ul>\n<h3>2.1 \u89e6\u53d1\u91cd\u6392\u7684\u5e38\u89c1\u64cd\u4f5c<\/h3>\n<pre><code class=\"language-javascript\">\n\/\/ \u4fee\u6539\u51e0\u4f55\u5c5e\u6027\nelement.style.width = &#x27;100px&#x27;;\nelement.style.height = &#x27;200px&#x27;;\nelement.style.margin = &#x27;10px&#x27;;\n\n\/\/ \u8bfb\u53d6\u5e03\u5c40\u5c5e\u6027\uff08\u5f3a\u5236\u540c\u6b65\u5e03\u5c40\uff09\nconst height = element.offsetHeight;\n<\/code><\/pre>\n<p>\u6ce8\u610f\uff1a\u8bfb\u53d6<code>offsetHeight<\/code>\u3001<code>getComputedStyle<\/code>\u7b49\u5c5e\u6027\u4f1a\u5f3a\u5236\u6d4f\u89c8\u5668\u7acb\u5373\u8ba1\u7b97\u5e03\u5c40\uff0c\u5982\u679c\u9891\u7e41\u8bfb\u5199\uff0c\u4f1a\u5bfc\u81f4<strong>\u5e03\u5c40\u6296\u52a8\uff08Layout Thrashing\uff09<\/strong>\u3002<\/p>\n<h3>2.2 \u907f\u514d\u5e03\u5c40\u6296\u52a8<\/h3>\n<pre><code class=\"language-javascript\">\n\/\/ \u4e0d\u597d\u7684\u505a\u6cd5\uff1a\u8bfb\u5199\u4ea4\u66ff\nfor (let i = 0; i &lt; 100; i++) {\n  const width = element.offsetWidth; \/\/ \u8bfb\n  element.style.width = width + 1 + &#x27;px&#x27;; \/\/ \u5199\n}\n\n\/\/ \u597d\u7684\u505a\u6cd5\uff1a\u6279\u91cf\u8bfb\u5199\nlet width = element.offsetWidth; \/\/ \u53ea\u8bfb\u4e00\u6b21\nfor (let i = 0; i &lt; 100; i++) {\n  width += 1;\n}\nelement.style.width = width + &#x27;px&#x27;; \/\/ \u53ea\u5199\u4e00\u6b21\n<\/code><\/pre>\n<h2>3. \u4f7f\u7528transform\u548copacity\u5b9e\u73b0\u9ad8\u6027\u80fd\u52a8\u753b<\/h2>\n<p>CSS\u7684<code>transform<\/code>\u548c<code>opacity<\/code>\u5c5e\u6027\u53ef\u4ee5\u5728\u5408\u6210\u5c42\u4e0a\u5904\u7406\uff0c\u4e0d\u4f1a\u89e6\u53d1\u91cd\u6392\u548c\u91cd\u7ed8\u3002\u56e0\u6b64\uff0c\u5bf9\u4e8e\u52a8\u753b\uff0c\u5e94\u4f18\u5148\u4f7f\u7528\u5b83\u4eec\u3002<\/p>\n<pre><code class=\"language-css\">\n\/* \u4e0d\u597d\u7684\u505a\u6cd5\uff1a\u6539\u53d8left\/top\u89e6\u53d1\u91cd\u6392 *\/\n.box {\n  position: absolute;\n  left: 0;\n  transition: left 0.3s;\n}\n.box:hover {\n  left: 100px;\n}\n\n\/* \u597d\u7684\u505a\u6cd5\uff1a\u4f7f\u7528transform *\/\n.box {\n  position: absolute;\n  left: 0;\n  transition: transform 0.3s;\n}\n.box:hover {\n  transform: translateX(100px);\n}\n<\/code><\/pre>\n<p>\ud83d\udca1 \u63d0\u793a\uff1a\u4f7f\u7528<code>will-change: transform;<\/code>\u53ef\u4ee5\u63d0\u524d\u544a\u77e5\u6d4f\u89c8\u5668\u63d0\u5347\u4e3a\u5408\u6210\u5c42\uff0c\u4f46\u4e0d\u8981\u6ee5\u7528\uff0c\u5426\u5219\u4f1a\u6d88\u8017\u5185\u5b58\u3002<\/p>\n<h2>4. \u4f18\u5316JavaScript\u6267\u884c<\/h2>\n<p>JavaScript\u6267\u884c\u4f1a\u963b\u585e\u6e32\u67d3\uff0c\u56e0\u6b64\u9700\u8981\u51cf\u5c11\u4e3b\u7ebf\u7a0b\u5de5\u4f5c\u91cf\u3002<\/p>\n<h3>4.1 \u4f7f\u7528requestAnimationFrame<\/h3>\n<p>\u5bf9\u4e8e\u52a8\u753b\uff0c\u4f7f\u7528<code>requestAnimationFrame<\/code>\uff08rAF\uff09\u53ef\u4ee5\u8ba9\u6d4f\u89c8\u5668\u5728\u4e0b\u4e00\u5e27\u4e4b\u524d\u6267\u884c\u56de\u8c03\uff0c\u907f\u514d\u4e22\u5e27\u3002<\/p>\n<pre><code class=\"language-javascript\">\nfunction animate() {\n  \/\/ \u66f4\u65b0\u52a8\u753b\u72b6\u6001\n  element.style.transform = `translateX(${x}px)`;\n  x += 1;\n  if (x &lt; 100) {\n    requestAnimationFrame(animate);\n  }\n}\nrequestAnimationFrame(animate);\n<\/code><\/pre>\n<h3>4.2 \u4f7f\u7528Web Worker\u5904\u7406\u5bc6\u96c6\u8ba1\u7b97<\/h3>\n<p>\u5c06\u8017\u65f6\u7684\u8ba1\u7b97\u79fb\u5165Web Worker\uff0c\u907f\u514d\u963b\u585eUI\u3002<\/p>\n<pre><code class=\"language-javascript\">\n\/\/ main.js\nconst worker = new Worker(&#x27;worker.js&#x27;);\nworker.postMessage({ data: largeArray });\nworker.onmessage = (e) =&gt; {\n  console.log(&#x27;\u8ba1\u7b97\u7ed3\u679c:&#x27;, e.data);\n};\n\n\/\/ worker.js\nself.onmessage = (e) =&gt; {\n  const result = heavyCompute(e.data);\n  self.postMessage(result);\n};\n<\/code><\/pre>\n<h2>5. \u4f7f\u7528Performance API\u5206\u6790\u6027\u80fd<\/h2>\n<p>\u6d4f\u89c8\u5668\u63d0\u4f9b\u4e86Performance API\uff0c\u53ef\u4ee5\u7cbe\u786e\u6d4b\u91cf\u6e32\u67d3\u6027\u80fd\u3002<\/p>\n<pre><code class=\"language-javascript\">\n\/\/ \u6d4b\u91cf\u51fd\u6570\u6267\u884c\u65f6\u95f4\nperformance.mark(&#x27;start&#x27;);\nheavyFunction();\nperformance.mark(&#x27;end&#x27;);\nperformance.measure(&#x27;heavyFunction&#x27;, &#x27;start&#x27;, &#x27;end&#x27;);\n\n\/\/ \u83b7\u53d6\u6d4b\u91cf\u7ed3\u679c\nconst measures = performance.getEntriesByName(&#x27;heavyFunction&#x27;);\nconsole.log(measures[0].duration);\n<\/code><\/pre>\n<p>\u7ed3\u5408Chrome DevTools\u7684Performance\u9762\u677f\uff0c\u53ef\u4ee5\u5f55\u5236\u5e76\u5206\u6790\u9875\u9762\u8fd0\u884c\u65f6\u6027\u80fd\uff0c\u627e\u51fa\u74f6\u9888\u3002<\/p>\n<h2>6. \u5173\u952e\u6e32\u67d3\u6307\u6807<\/h2>\n<ul>\n<li><strong>FP (First Paint)<\/strong>\uff1a\u9996\u6b21\u7ed8\u5236\u65f6\u95f4\u3002<\/li>\n<li><strong>FCP (First Contentful Paint)<\/strong>\uff1a\u9996\u6b21\u5185\u5bb9\u7ed8\u5236\u65f6\u95f4\u3002<\/li>\n<li><strong>LCP (Largest Contentful Paint)<\/strong>\uff1a\u6700\u5927\u5185\u5bb9\u7ed8\u5236\u65f6\u95f4\uff0c\u8861\u91cf\u52a0\u8f7d\u6027\u80fd\u3002<\/li>\n<li><strong>CLS (Cumulative Layout Shift)<\/strong>\uff1a\u7d2f\u79ef\u5e03\u5c40\u504f\u79fb\uff0c\u8861\u91cf\u89c6\u89c9\u7a33\u5b9a\u6027\u3002<\/li>\n<li><strong>FID (First Input Delay)<\/strong>\uff1a\u9996\u6b21\u8f93\u5165\u5ef6\u8fdf\uff0c\u8861\u91cf\u4ea4\u4e92\u6027\u3002<\/li>\n<\/ul>\n<p>\u4f18\u5316\u8fd9\u4e9b\u6307\u6807\u53ef\u4ee5\u663e\u8457\u63d0\u5347\u7528\u6237\u4f53\u9a8c\u3002\u4f8b\u5982\uff0c\u4e3a\u56fe\u7247\u8bbe\u7f6e\u5bbd\u9ad8\u6bd4\u53ef\u4ee5\u907f\u514dCLS\uff0c\u4f7f\u7528<code>font-display: swap<\/code>\u907f\u514d\u5b57\u4f53\u963b\u585e\u6e32\u67d3\u3002<\/p>\n<h2>7. \u5b9e\u6218\uff1a\u4f18\u5316\u4e00\u4e2a\u5361\u987f\u7684\u5217\u8868\u6e32\u67d3<\/h2>\n<p>\u5047\u8bbe\u6709\u4e00\u4e2a\u957f\u5217\u8868\uff0c\u6bcf\u6b21\u6eda\u52a8\u90fd\u4f1a\u52a8\u6001\u52a0\u8f7d\u9879\uff0c\u4f46\u6eda\u52a8\u65f6\u51fa\u73b0\u5361\u987f\u3002<\/p>\n<p><strong>\u95ee\u9898\u4ee3\u7801\uff1a<\/strong><\/p>\n<pre><code class=\"language-javascript\">\nwindow.addEventListener(&#x27;scroll&#x27;, () =&gt; {\n  const items = document.querySelectorAll(&#x27;.item&#x27;);\n  items.forEach(item =&gt; {\n    const rect = item.getBoundingClientRect();\n    if (rect.top &lt; window.innerHeight) {\n      item.classList.add(&#x27;visible&#x27;);\n    }\n  });\n});\n<\/code><\/pre>\n<p>\u6bcf\u6b21\u6eda\u52a8\u90fd\u8bfb\u53d6<code>getBoundingClientRect<\/code>\uff0c\u89e6\u53d1\u5f3a\u5236\u540c\u6b65\u5e03\u5c40\u3002<\/p>\n<p><strong>\u4f18\u5316\u65b9\u6848\uff1a<\/strong><\/p>\n<ol>\n<li>\u4f7f\u7528<code>IntersectionObserver<\/code>\u66ff\u4ee3\u6eda\u52a8\u76d1\u542c\u3002<\/li>\n<li>\u4f7f\u7528<code>requestAnimationFrame<\/code>\u8282\u6d41\u3002<\/li>\n<\/ol>\n<pre><code class=\"language-javascript\">\nconst observer = new IntersectionObserver((entries) =&gt; {\n  entries.forEach(entry =&gt; {\n    if (entry.isIntersecting) {\n      entry.target.classList.add(&#x27;visible&#x27;);\n      observer.unobserve(entry.target);\n    }\n  });\n});\n\ndocument.querySelectorAll(&#x27;.item&#x27;).forEach(item =&gt; observer.observe(item));\n<\/code><\/pre>\n<h2>8. \u603b\u7ed3<\/h2>\n<p>\u6d4f\u89c8\u5668\u6e32\u67d3\u6027\u80fd\u4f18\u5316\u662f\u4e00\u4e2a\u7cfb\u7edf\u5de5\u7a0b\u3002\u5173\u952e\u70b9\uff1a<\/p>\n<ul>\n<li>\u51cf\u5c11\u91cd\u6392\u548c\u91cd\u7ed8\uff0c\u4f18\u5148\u4f7f\u7528<code>transform<\/code>\u548c<code>opacity<\/code>\u3002<\/li>\n<li>\u907f\u514d\u5e03\u5c40\u6296\u52a8\uff0c\u6279\u91cf\u8bfb\u5199\u6837\u5f0f\u3002<\/li>\n<li>\u4f7f\u7528<code>requestAnimationFrame<\/code>\u548cWeb Worker\u3002<\/li>\n<li>\u5229\u7528Performance API\u548cDevTools\u5b9a\u4f4d\u74f6\u9888\u3002<\/li>\n<li>\u5173\u6ce8\u6838\u5fc3Web\u6307\u6807\u3002<\/li>\n<\/ul>\n<p>\u5ef6\u4f38\u9605\u8bfb\uff1a<\/p>\n<ul>\n<li><a href=\"https:\/\/developers.google.com\/web\/fundamentals\/performance\/rendering\">\u6e32\u67d3\u6027\u80fd &#8211; Web Fundamentals<\/a><\/li>\n<li><a href=\"https:\/\/csstriggers.com\/\">CSS Triggers<\/a><\/li>\n<li><a href=\"https:\/\/www.w3.org\/webperf\/\">Web Performance Working Group<\/a><\/li>\n<\/ul>\n<p>\u5e0c\u671b\u672c\u6587\u80fd\u5e2e\u52a9\u4f60\u6784\u5efa\u66f4\u6d41\u7545\u7684Web\u5e94\u7528\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u6df1\u5165\u89e3\u6790\u6d4f\u89c8\u5668\u6e32\u67d3\u6d41\u7a0b\uff0c\u901a\u8fc7\u4ee3\u7801\u793a\u4f8b\u6f14\u793a\u5982\u4f55\u4f18\u5316\u91cd\u6392\u3001\u91cd\u7ed8\u4e0e\u5408\u6210\uff0c\u5e76\u4ecb\u7ecd\u5173\u952e\u6027\u80fd\u6307\u6807\u4e0e\u5de5\u5177\uff0c\u5e2e\u52a9\u5f00\u53d1\u8005\u6784\u5efa\u6d41\u7545\u7684Web\u5e94\u7528\u3002<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[3,4],"tags":[256,49,6,192,294],"class_list":["post-298","post","type-post","status-publish","format-standard","hentry","category-3","category-4","tag-web","tag-49","tag-6","tag-192","tag-294"],"_links":{"self":[{"href":"https:\/\/yykcj.com\/index.php?rest_route=\/wp\/v2\/posts\/298","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=298"}],"version-history":[{"count":0,"href":"https:\/\/yykcj.com\/index.php?rest_route=\/wp\/v2\/posts\/298\/revisions"}],"wp:attachment":[{"href":"https:\/\/yykcj.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=298"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/yykcj.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=298"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/yykcj.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=298"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}