{"id":9537,"date":"2020-10-11T17:31:49","date_gmt":"2020-10-11T09:31:49","guid":{"rendered":"https:\/\/www.zhangxinxu.com\/wordpress\/?p=9537"},"modified":"2020-12-01T23:47:19","modified_gmt":"2020-12-01T15:47:19","slug":"css-var-custom-any-function-polyfill","status":"publish","type":"post","link":"https:\/\/www.zhangxinxu.com\/wordpress\/2020\/10\/css-var-custom-any-function-polyfill\/","title":{"rendered":"\u5de7\u501fCSS var\u53d8\u91cf\u5b9e\u73b0\u4efb\u610f\u7684CSS\u81ea\u5b9a\u4e49\u8bed\u6cd5"},"content":{"rendered":"<p>by <a href=\"https:\/\/www.zhangxinxu.com\/\">zhangxinxu<\/a> from <a href=\"https:\/\/www.zhangxinxu.com\/wordpress\/?p=9537\">https:\/\/www.zhangxinxu.com\/wordpress\/?p=9537<\/a><br \/>\n\u672c\u6587\u6b22\u8fce\u5206\u4eab\u4e0e\u805a\u5408\uff0c\u5168\u6587\u8f6c\u8f7d\u5c31\u4e0d\u5fc5\u4e86\uff0c\u5c0a\u91cd\u7248\u6743\uff0c\u5708\u5b50\u5c31\u8fd9\u4e48\u5927\uff0c\u82e5\u6025\u7528\u53ef\u4ee5\u8054\u7cfb\u6388\u6743\u3002<\/p>\n<p><script>window.wxShareImgUrl = 'https:\/\/www.zhangxinxu.com\/study\/image\/share\/202020-css-var-polyfill.png';<\/script><\/p>\n<p><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/image.zhangxinxu.com\/image\/blog\/202010\/css-var-polyfill-cover.png\" width=\"480\" height=\"304\" alt=\"CSS\u53d8\u91cf\u7684\u81ea\u5b9a\u4e49\u8bed\u6cd5\u6280\u672f\" class=\"alignright clip thumb size-medium\" \/><\/p>\n<p>CSS\u81ea\u5b9a\u4e49\u5c5e\u6027\u652f\u6301\u4efb\u610f\u7c7b\u578b\u7684\u5c5e\u6027\u503c\uff0c\u6211\u4eec\u53ef\u4ee5\u501f\u52a9\u8fd9\u4e00\u7279\u6027\u81ea\u5b9a\u4e49CSS\u8bed\u6cd5\uff0c\u6216\u8005polyfill\u5168\u65b0\u7684CSS\u8bed\u6cd5\u3002<\/p>\n<p>\u672c\u6587\u901a\u8fc73\u4e2a\u6848\u4f8b\u5c55\u793a\u8fd9\u79cd\u7528\u6cd5\u3002<\/p>\n<h3>\u4e00\u3001CSS\u53d8\u91cf\u81ea\u5b9a\u4e49\u5168\u65b0\u7684CSS\u8bed\u6cd5<\/h3>\n<p>CSS\u65b0\u4e16\u754c\u4e2d\u7684\u65b0\u7279\u6027\u867d\u7136\u5f88\u591a\uff0c\u4f46\u662f\u4ecd\u7136\u6709\u90e8\u5206\u9700\u6c42\u662f\u65e0\u6cd5\u5b9e\u73b0\u7684\uff0c\u4f8b\u5982\u867d\u7136\u76ee\u524dCSS Color Level 4\u652f\u6301\u8d85\u7ea7\u591a\u7684\u989c\u8272\u5199\u6cd5\uff08\u8be6\u89c1<a href=\"https:\/\/www.zhangxinxu.com\/wordpress\/2020\/10\/css-color-level-4\/\">\u6211\u5199\u7684\u8fd9\u7bc7\u6587\u7ae0<\/a>\uff09\uff0c\u4f46\u662f\u552f\u72ec\u4e0d\u652f\u6301\u989c\u8272\u5173\u952e\u5b57\u7684\u534a\u900f\u660e\u6548\u679c\u3002<\/p>\n<p>\u6240\u4ee5\uff0c\u6211\u5c31\u4f1a\u601d\u8003\u8fd9\u6837\u4e00\u4e2a\u95ee\u9898\uff0c\u6709\u6ca1\u6709\u53ef\u80fd\u81ea\u521b\u4e00\u4e2aCSS\u51fd\u6570\u8bed\u6cd5\uff0c\u5b9e\u73b0\u989c\u8272\u5173\u952e\u5b57\u7684\u534a\u900f\u660e\u8bbe\u7f6e\uff0c\u4f8b\u5982\u81ea\u521b\u4e00\u4e2a<code>keyword()<\/code>\u51fd\u6570\uff1a<\/p>\n<pre>color: keyword(red, 50%)\r\ncolor: keyword(red, 0.5)\r\ncolor: keyword(red \/ 50%)\r\ncolor: keyword(red \/ 0.5)<\/pre>\n<p>\u663e\u7136\uff0c\u4e0a\u9762\u7684\u8bed\u6cd5\u6d4f\u89c8\u5668\u80af\u5b9a\u662f\u65e0\u6cd5\u8bc6\u522b\u7684\uff0c\u56fe8-7\u5c31\u662fChrome\u6d4f\u89c8\u5668\u4e0b\u8bed\u6cd5\u65e0\u6548\u7684\u622a\u56fe\u793a\u610f\u3002<\/p>\n<div class=\"figure\">\n    <img decoding=\"async\" src=\"https:\/\/image.zhangxinxu.com\/image\/blog\/202010\/8-7_keyword-var-fail.png\" loading=\"lazy\" width=\"400\" height=\"183\"><\/p>\n<p><small>\u56fe8-7 \u81ea\u5b9a\u4e49\u7684<code>keyword()<\/code>\u51fd\u6570\u65e0\u6548\u793a\u610f<\/small><\/p>\n<\/div>\n<p>\u90a3\u6709\u6ca1\u6709\u529e\u6cd5\u8ba9\u6d4f\u89c8\u5668\u8ba4\u4e3a<code>keyword()<\/code>\u51fd\u6570\u4e5f\u662f\u5408\u6cd5\u7684\u5462\uff1f<\/p>\n<p>\u8fd8\u771f\u6709\uff0c\u90a3\u5c31\u662f\u4f7f\u7528CSS\u81ea\u5b9a\u4e49\u5c5e\u6027\uff0c\u8ba9CSS\u81ea\u5b9a\u4e49\u5c5e\u6027\u505a\u4e3a\u4fe1\u4f7f\u8ba9\u6574\u4e2a\u8bed\u6cd5\u5408\u6cd5\u5316\uff0c\u4f8b\u5982\uff1a<\/p>\n<pre>body {\r\n    --keyword: keyword(red, 50%);  <span style=\"color:green;\">\/* \u5408\u6cd5 *\/<\/span>\r\n    color: var(--keyword);\r\n}<\/pre>\n<p>\u6b64\u65f6\u6d4f\u89c8\u5668\u4f1a\u8ba4\u4e3a\u4e0a\u9762\u7684<code>color<\/code>\u5c5e\u6027\u8bed\u6cd5\u5c31\u662f\u5408\u6cd5\u7684\uff0c\u4e0b\u56fe\u5c31\u662fChrome\u6d4f\u89c8\u5668\u4e0b\u8bed\u6cd5\u5408\u6cd5\u7684\u622a\u56fe\u793a\u610f\u3002<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/image.zhangxinxu.com\/image\/blog\/202010\/8-8_keyword-var-ok.png\" loading=\"lazy\" width=\"400\" height=\"103\" alt=\"keyword()\u51fd\u6570\u8bed\u6cd5\u6709\u6548\u793a\u610f\"><\/p>\n<p>\u63a5\u4e0b\u6765\u8981\u505a\u7684\u4e8b\u60c5\u5c31\u7b80\u5355\u4e86\uff0c\u65e2\u7136\u8bed\u6cd5\u5408\u6cd5\uff0c\u90a3\u6211\u4eec\u5c31\u53ef\u4ee5\u4f7f\u7528JavaScript\u83b7\u53d6\u5230\u54ea\u4e9b\u5143\u7d20\u4f7f\u7528\u4e86<code>keyword()<\/code>\u51fd\u6570\uff0c\u6b64\u65f6\u518d\u8f6c\u6362\u6210\u6d4f\u89c8\u5668\u53ef\u8bc6\u522b\u7684\u989c\u8272\u51fd\u6570\u5c31\u53ef\u4ee5\u5b9e\u73b0\u6211\u4eec\u60f3\u8981\u7684\u6548\u679c\u4e86\u3002<\/p>\n<p>\u5177\u4f53\u5b9e\u73b0\u539f\u7406\u5982\u4e0b\u6240\u793a\uff1a<\/p>\n<ol>\n<li>\u83b7\u53d6\u2eda\u2faf\u6240\u6709\u7684\u5305\u542b<code>keyword()<\/code>\u7684\u2f83\u5b9a\u4e49\u5c5e\u6027\uff0c\u5c31\u662f\u628a\u4ee3\u7801\u4e2d\u7684CSS\u53d8\u6210JS\u4e2d\u7684\u6570\u7ec4\u6570\u636e\uff0c\u7c7b\u4f3c\u8fd9\u6837\uff1a\n<pre>body {\r\n --keyword: keyword(red, 50%);\r\n color: var(--keyword);\r\n}<\/pre>\n<p>\u7136\u540e\u53d8\u6210\uff1a<\/p>\n<pre>[\r\n ['--keyword', 'keyword(red, 50%)]\r\n]<\/pre>\n<\/li>\n<li>\u904d\u5386\u5e76\u89c2\u5bdf\u6240\u6709DOM\uff0c\u5982\u679c\u8bbe\u7f6e\u4e86\u5bf9\u5e94\u7684\u2f83\u5b9a\u4e49\u5c5e\u6027\uff0c\u5c06<code>keyword()<\/code>\u8bed\u6cd5\u8f6c\u6362\u6210\u6d4f\u89c8\u5668\u8bc6\u522b\u7684<code>rgba()<\/code>\u8bed\u6cd5\u3002\u4f8b\u5982\uff1a<br \/>\n<img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/image.zhangxinxu.com\/image\/blog\/202010\/2020-10-11_123905.png\" width=\"577\" height=\"134\" alt=\"\u81ea\u5b9a\u4e49\u5c5e\u6027\u7684\u904d\u5386\u4e0e\u5904\u7406\" class=\"alignnone size-medium\" \/><\/li>\n<\/ol>\n<p>\u57fa\u4e8e\u4e0a\u8ff0\u539f\u7406\uff0c\u6211\u5199\u4e86\u4e2aJavaScript\u4ee3\u7801\u7247\u6bb5\uff0c\u5927\u5bb6\u53ea\u9700\u8981\u5728\u9875\u9762\u4e2d\u5f15\u5165\u4e0b\u9762\u8fd9\u6bb5HTML\u4ee3\u7801\uff0c\u5c31\u53ef\u4ee5\u7545\u5feb\u81ea\u5982\u5730\u4f7f\u7528\u81ea\u5b9a\u4e49\u7684<code>keyword()<\/code>\u51fd\u6570\u4e86\u3002<\/p>\n<pre>&lt;script src=\".\/keyword-color.js\"&gt;&lt;\/script&gt;<\/pre>\n<p>keyword-color.js\u53ef\u4ee5\u70b9\u51fb\u6216\u53f3\u952e\u8fd9\u91cc\u8fdb\u884c\u4e0b\u8f7d\uff1a<a href=\"https:\/\/www.zhangxinxu.com\/study\/202008\/keyword-color.js\" rel=\"noopener\" target=\"_blank\">keyword-color.js<\/a><\/p>\n<p>\u4e0b\u9762\u7b80\u5355\u6d4b\u8bd5\u4e0b\uff0c\u5047\u8bbe\u9875\u9762\u4e2d\u6709\u5982\u4e0b\u6240\u793a\u7684HTML\u548cCSS\u4ee3\u7801\uff1a<\/p>\n<pre>&lt;body&gt;\r\n    &lt;p&gt;\u6587\u5b57\u989c\u8272\u662f\uff1f&lt;\/p&gt;\r\n&lt;\/body&gt;<\/pre>\n<pre>body {\r\n    --keyword: keyword(blue, 50%);\r\n    color: var(--keyword);\r\n    <span class=\"green\">\/* \u652f\u6301\u81ea\u5b9a\u4e49\u5c5e\u6027\u5d4c\u5957 *\/<\/span>\r\n    --aaa: keyword(blue, 0.1);\r\n    --bbb: var(--aaa);\r\n    background-color: var(--bbb);\r\n\r\n    font-size: 3rem;\r\n}<\/pre>\n<p>\u7ed3\u679c\u5c31\u6709\u5982\u4e0b\u56fe\u6240\u793a\u7684\u6548\u679c\uff0c\u65e0\u8bba\u662f\u80cc\u666f\u8272\u8fd8\u662f\u6587\u5b57\u989c\u8272\u90fd\u8868\u73b0\u51fa\u4e86\u7b26\u5408\u9884\u671f\u7684\u534a\u900f\u660e\u6548\u679c\u3002<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/image.zhangxinxu.com\/image\/blog\/202010\/8-9_keyword-var-effect.png\" loading=\"lazy\" width=\"351\" height=\"188\" alt=\"\u81ea\u5b9a\u4e49\u7684keyword()\u51fd\u6570\u8bed\u6cd5\u751f\u6548\u793a\u610f\"><\/p>\n<p><span class=\"hidden\">\/\/zxx: \u5982\u679c\u4f60\u770b\u5230\u8fd9\u6bb5\u6587\u5b57\uff0c\u8bf4\u660e\u4f60\u73b0\u5728\u8bbf\u95ee\u662f\u4f53\u9a8c\u7cdf\u7cd5\u7684\u5783\u573e\u76d7\u7248\u7f51\u7ad9\uff0c\u4f60\u53ef\u4ee5\u8bbf\u95ee\u539f\u6587\u83b7\u5f97\u5f88\u597d\u7684\u4f53\u9a8c\uff1ahttps:\/\/www.zhangxinxu.com\/wordpress\/?p=9537\uff08\u4f5c\u8005\u5f20\u946b\u65ed\uff09<\/span><\/p>\n<p>\u773c\u89c1\u4e3a\u5b9e\uff0c\u60a8\u53ef\u4ee5\u72e0\u72e0\u5730\u70b9\u51fb\u8fd9\u91cc\uff1a<a href=\"https:\/\/www.zhangxinxu.com\/study\/202008\/keyword-color.html\" rel=\"noopener\" target=\"_blank\">\u81ea\u5b9a\u4e49keyword()\u8bed\u6cd5\u4e0e\u6548\u679cdemo<\/a><\/p>\n<h3>\u4e8c\u3001Polyfill\u6848\u4f8b\u4e4boffset-path<\/h3>\n<p>\u4e0a\u9762\u7684\u81ea\u5b9a\u4e49<code>keyword()<\/code>\u51fd\u6570\u8bed\u6cd5\u7684\u6848\u4f8b\u5176\u5b9e\u4f7f\u7528\u9884\u5904\u7406\u4e5f\u53ef\u4ee5\u89e3\u51b3\u7684\uff0c\u7c7b\u4f3cSass\uff0cLess\u8fd9\u79cd\uff0c\u76f4\u63a5\u5199\u4e2a\u7f16\u8bd1\u51fd\u6570\uff0c\u4f7f\u7528Node.js\u8f6c\u6362\u6210rgba()\u51fd\u6570\uff0c\u4e5f\u662f\u5f88\u9999\u7684\u3002<\/p>\n<p>\u4f8b\u5982\uff1a<\/p>\n<pre>body {\r\n --keyword: keyword(red, 50%);\r\n color: var(--keyword);\r\n}<\/pre>\n<p>\u4f7f\u7528Node.js\u5de5\u5177\u76f4\u63a5\u8f6c\u6362\u6210\uff1a<\/p>\n<pre>body {\r\n color: rgba(255,0,0,.5);\r\n}<\/pre>\n<p>\u4f46\u662f\uff0c\u6709\u4e00\u7c7bCSS\u8bed\u6cd5\u7684\u81ea\u5b9a\u4e49\u9884\u5904\u7406\u5de5\u5177\u662f\u641e\u4e0d\u5b9a\u7684\uff0c\u90a3\u5c31\u662f\u4e00\u4e9b\u8fd8\u6ca1\u6709\u652f\u6301\u7684CSS\u65b0\u8bed\u6cd5\u7684Polyfill\u5b9e\u73b0\uff0c\u8fd9\u662fCSS\u53d8\u91cf\u4f5c\u4e3a\u4e2d\u95f4\u4ef6\u81ea\u5b9a\u4e49\u5176\u4ed6CSS\u8bed\u6cd5\u6280\u672f\u7684\u738b\u724c\u5e94\u7528\u3002<\/p>\n<p>\u8fd9\u91cc\u5148\u4e3e<code>offset-path<\/code>\u5c5e\u6027\u7684\u4f8b\u5b50\u3002<\/p>\n<p>\u5927\u5bb6\u5e94\u8be5\u77e5\u9053\uff0c<code>offset-path<\/code>\u2f40\u6301<code>path()<\/code>\u51fd\u6570\u53ef\u4ee5\u5b9e\u73b0\u5143\u7d20\u4efb\u610f\u4e0d\u89c4\u5219\u8def\u5f84\u7684\u8fd0\u52a8\u6548\u679c\u3002<\/p>\n<pre>.horse-run {\r\n offset-path: path(\"M10,80 q100,120 120,20 q140,-50 160,0\");\r\n animation: move 3s linear infinite;\r\n}\r\n@keyframes move {\r\n 100% { offset-distance: 100%;}\r\n}<\/pre>\n<p>\u5c31\u4f1a\u6709\u4e00\u4e2a\u9a6c\u513f\u8dd1\u8d77\u6765\u7684\u6548\u679c\u3002<\/p>\n<p><img decoding=\"async\" loading=\"lazy\" src=\"\/\/image.zhangxinxu.com\/image\/blog\/201703\/horse-run.gif\" width=\"311\" height=\"141\" alt=\"\u9a6c\u513f\u4e0d\u505c\u8dd1gif\u622a\u56fe\" class=\"alignnone\"><\/p>\n<p>\u5173\u4e8e<code>offset-path<\/code>\u5c5e\u6027\u66f4\u591a\u5185\u5bb9\u53ef\u4ee5\u53c2\u89c1\u8fd9\u7bc7\u6587\u7ae0\uff1a\u201c<a href=\"https:\/\/www.zhangxinxu.com\/wordpress\/2017\/03\/offset-path-css-animation\/\">\u4f7f\u7528CSS offset-path\u8ba9\u5143\u7d20\u6cbf\u7740\u4e0d\u89c4\u5219\u8def\u5f84\u8fd0\u52a8<\/a>\u201d\u3002<\/p>\n<p>\u5b9e\u9645\u4e0a\uff0c<code>offset-path<\/code>\u7684\u89c4\u8303\u8fd8\u652f\u6301\u5f88\u591a\u5176\u4ed6\u7684CSS\u51fd\u6570\uff0c\u4f8b\u5982<code>url()<\/code>\u51fd\u6570\uff1a<\/p>\n<pre>offset-path: url(#somePathId)<\/pre>\n<p>\u6216\u8005\u57fa\u672c\u56fe\u5f62\u8fd0\u52a8\u51fd\u6570\uff1a<\/p>\n<pre>offset-path: circle(50% at 25% 25%);\r\noffset-path: ellipse(50% 40px at top);\r\noffset-path: inset(50% 50% 50% 50%);\r\noffset-path: polygon(30% 0%, 70% 0%, 30% 100%, 0% 70%, 0% 30%);<\/pre>\n<p>\u53ef\u60dc\u76ee\u524d\u6240\u6709\u6d4f\u89c8\u5668\u90fd\u4e0d\u652f\u6301\uff0c\u5982\u4e0b\u56fe\u6240\u793a\uff1a<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/image.zhangxinxu.com\/image\/blog\/202010\/2020-10-11_125203.png\" width=\"516\" height=\"213\" alt=\"offset-path\u5176\u4ed6\u51fd\u6570\u90fd\u4e0d\u652f\u6301\" class=\"alignnone size-medium\" \/><\/p>\n<p>\u6ca1\u5173\u7cfb\uff0c\u6211\u4eec\u53ef\u4ee5\u501f\u52a9CSS\u53d8\u91cf\u8fdb\u884cPolyfill\u3002<\/p>\n<p>\u9996\u5148\u6240\u6709\u51fd\u6570\u4f7f\u7528CSS\u81ea\u5b9a\u4e49\u5c5e\u6027\u8868\u793a\uff0c\u5982\u4e0b\u6240\u793a\uff1a<\/p>\n<pre>--offset-path: url(#somePathId);\r\n--offset-path: circle();\r\n--offset-path: ellipse();\r\n--offset-path: inset();\r\n--offset-path: polygon(30% 0%, 70% 0%, 0% 30%);\r\noffset-path: var(--offset-path);\r\n<\/pre>\n<p>\u7136\u540e\u4f7f\u7528JavaScript\u8bc6\u522b\u54ea\u4e9b\u5143\u7d20\u5e94\u7528\u4e86\u8fd9\u4e9bCSS\u81ea\u5b9a\u4e49\u5c5e\u6027\uff0c\u7136\u540e\u8fdb\u884c\u8bed\u6cd5\u5206\u6790\uff0c\u8f6c\u6362\u6210\u6d4f\u89c8\u5668\u652f\u6301\u7684<code>path()<\/code>\u51fd\u6570\u5373\u53ef\u3002<\/p>\n<p>\u773c\u89c1\u4e3a\u5b9e\uff0c\u60a8\u53ef\u4ee5\u72e0\u72e0\u5730\u70b9\u51fb\u8fd9\u91cc\uff1a<a href=\"https:\/\/www.zhangxinxu.com\/study\/202008\/offset-path-polyfill.php\" rel=\"noopener\" target=\"_blank\">offset-path polyfill\u65b9\u6cd5\u6d4b\u8bd5demo<\/a><\/p>\n<p>\u5404\u4e2a\u51fd\u6570polyfill\u4e4b\u540e\u7684\u6548\u679c\u5982\u4e0bGIF\u6240\u793a\uff08\u70b9\u51fb\u64ad\u653e 278K\uff09\uff1a<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/image.zhangxinxu.com\/image\/blog\/202010\/offset-path-polyfill.png\" width=\"354\" height=\"787\" alt=\"\u8def\u5f84\u8fd0\u52a8\" class=\"alignnone size-medium\" onclick=\"this.src=this.src.replace('.png', '.gif')\" \/><\/p>\n<h4>\u5982\u4f55\u4f7f\u7528\uff1f<\/h4>\n<p>1. \u5f15\u5165JS\uff0c\u4f8b\u5982\uff1a<\/p>\n<pre>&lt;script src=\".\/offset-path.js\"&gt;&lt;\/script&gt;<\/pre>\n<p>offset-path.js\u53ef\u4ee5\u70b9\u51fb\u6216\u53f3\u952e\u8fd9\u91cc\u8fdb\u884c\u4e0b\u8f7d\uff1a<a href=\"https:\/\/www.zhangxinxu.com\/study\/202008\/offset-path.js\" rel=\"noopener\" target=\"_blank\">keyword-color.js<\/a><\/p>\n<p>2. \u9700\u8981\u4f7f\u7528<code>offset-path<\/code>\u5176\u4ed6\u51fd\u6570\u8bed\u6cd5\u7684\u5143\u7d20\u65b0\u589e\u4e00\u4e2a\u81ea\u5b9a\u4e49\u5c5e\u6027is-offset-path\uff0c\u4f8b\u5982\uff1a<\/p>\n<pre>&lt;img src=\"horse.png\" class=\"horse-run\" <span style=\"color:#cd0000;\">is-offset-path<\/span> title=\"by zhangxinxu(.com)\"&gt;<\/pre>\n<p>3. \u4f7f\u7528CSS\u81ea\u5b9a\u4e49\u5c5e\u6027\u8bbe\u7f6e\u76f8\u5173\u7684\u8fd0\u52a8\u8def\u5f84\u51fd\u6570\uff0c\u4f8b\u5982\u8fd9\u91cc\u4f7f\u7528\u5916\u90e8\u7684SVG\u5143\u7d20\u8def\u5f84\uff1a<\/p>\n<pre>.horse-run {\r\n --offset-path: url(#road);\r\n offset-path: var(--offset-path);\r\n animation: motion 3s linear infinite;\r\n}<\/pre>\n<p>\u9875\u9762\u4e2d\u7684SVG\u5143\u7d20\u4ee3\u7801\u5982\u4e0b\uff08\u5176\u4ed6\u51fd\u6570\u4e0d\u9700\u8981SVG\u652f\u6301\uff09\uff1a<\/p>\n<pre>&lt;svg width=\"280\" height=\"150\" viewBox=\"0 0 280 150\"&gt;\r\n &lt;path id=\"road\" d=\"M10,80 q100,120 120,20 q140,-50 160,0\"\/&gt;\r\n&lt;\/svg&gt;<\/pre>\n<p>4. \u6548\u679c\u5b8c\u6210\u3002\u6b64\u65f6\uff0coffset-path.js\u4f1a\u628a<code>url(#road)<\/code>\u8f6c\u6362\u6210\u6d4f\u89c8\u5668\u53ef\u4ee5\u8bc6\u522b\u7684<code>path()<\/code>\u51fd\u6570\uff0c\u4e8e\u662f\u5c31\u6709\u4e86\u5bf9\u5e94\u7684\u8fd0\u52a8\u6548\u679c\u3002<\/p>\n<p>\u6e90\u4ee3\u7801\u622a\u56fe\u5982\u4e0b\u6240\u793a\uff1a<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/image.zhangxinxu.com\/image\/blog\/202010\/2020-10-11_165937.png\" width=\"728\" height=\"279\" alt=\"\u8f6c\u6362\u6210path()\u51fd\u6570\u793a\u610f\" class=\"alignnone size-medium\" \/><\/p>\n<p><strong>\u8865\u5145\u4e8e2020-12-01<\/strong><\/p>\n<p>offset-path.js\u6211\u521a\u521a\u5728gitee\u4e0a\u5f00\u6e90\u4e86\uff0c\u65b9\u4fbf\u5927\u5bb6\u5b66\u4e60\u548c\u4f7f\u7528\uff1a<a href=\"https:\/\/gitee.com\/zhangxinxu\/css-offset-polyfill\" rel=\"noopener\" target=\"_blank\" class=\"a_link\">https:\/\/gitee.com\/zhangxinxu\/css-offset-polyfill<\/a><\/p>\n<p>\u4e5f\u6b22\u8fce\u5927\u5bb6\u5173\u6ce8\u6211\u7684gitee\u8d26\u53f7\uff0c\u4ee5\u540e\u975e\u56fd\u9645\u5316\u7684\u9879\u76ee\u6211\u90fd\u4f1a\u5728gitee\u5f00\u6e90\u3002<\/p>\n<h3>\u4e09\u3001attr()\u51fd\u6570\u65b0\u8bed\u6cd5\u7684\u652f\u6301<\/h3>\n<p>\u8fd9\u4e2a\u6848\u4f8b\u4e0a\u4e00\u7bc7\u6587\u7ae0\u4e13\u95e8\u8bb2\u8fc7\u3002<\/p>\n<p><code>attr()<\/code>\u51fd\u6570\u65b0\u8bed\u6cd5\u53ef\u4ee5\u8ba9HTML\u5c5e\u6027\u53d8\u6210\u4efb\u610f\u7684CSS\u503c\uff0c\u4f8b\u5982\u53ef\u4ee5\u8ba9HTML\u5c5e\u6027\u4e2d\u7684\u4efb\u610f\u5c5e\u6027\u7684\u989c\u8272\u503c\u8f6c\u53d8\u6210CSS\u4e2d\u771f\u5b9e\u7684\u503c\u8fdb\u884c\u4f7f\u7528\uff0c\u4f8b\u5982\uff1a<\/p>\n<pre><button bgcolor=\"skyblue\" radius=\"4\">\u6309\u94ae<\/button>\r\n<button bgcolor=\"#00000040\" radius=\"1rem\">\u6309\u94ae<\/button>\r\n<button bgcolor=\"red\" radius=\"50%\">\u6309\u94ae<\/button><\/pre>\n<pre>button {\r\n    background-color: attr(bgcolor color);\r\n    border-radius: attr(radius px, 4px);\r\n}<\/pre>\n<p>\u7406\u8bba\u4e0a\u4f1a\u6709\u4e0b\u56fe\u6240\u793a\u7684\u6548\u679c\u3002<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/image.zhangxinxu.com\/image\/blog\/202010\/2020-10-11_170936.png\" width=\"305\" height=\"66\" alt=\"\u7406\u8bba\u4e0a\u7684\u6309\u94ae\u6548\u679c\u793a\u610f\" class=\"alignnone size-medium\" \/><\/p>\n<p>\u53ef\u4ee5\u770b\u5230<code>attr()<\/code>\u51fd\u6570\u65b0\u8bed\u6cd5\u8ba9HTML\u548cCSS\u4e4b\u95f4\u7684\u8054\u7cfb\u66f4\u52a0\u7d27\u5bc6\uff0c\u8fd9\u4e2a\u7279\u6027\u6211\u8ba4\u4e3a\u662f\u4f1a\u5e26\u6765\u5f88\u591a\u53d8\u9769\u4e0e\u98a0\u8986\u7684\uff0c\u4f46\u662f\u6ca1\u6709\u4efb\u4f55\u6d4f\u89c8\u5668\u652f\u6301\uff0c\u4e14\u4f30\u8ba1\u5f88\u957f\u4e00\u6bb5\u65f6\u95f4\u4e5f\u4e0d\u4f1a\u6709\u6d4f\u89c8\u5668\u652f\u6301\u3002<\/p>\n<p>\u4e0d\u8981\u6025\uff0c\u501f\u52a9CSS\u53d8\u91cf\uff0c\u6211\u4eec\u540c\u6837\u53ef\u4ee5\u6d4f\u89c8\u5668\u80fd\u8bc6\u522b\u6211\u4eec\u4e66\u5199\u7684CSS <code>attr()<\/code>\u51fd\u6570\u65b0\u8bed\u6cd5\u3002<\/p>\n<p>\u4f8b\u5982\uff1a<\/p>\n<pre>button {\r\n    --attr-bg: attr(bgcolor color);\r\n    background-color: var(--attr-bg);\r\n    --attr-radius: attr(radius px, 4px);\r\n    border-radius: var(--attr-radius);\r\n}<\/pre>\n<p>\u524d\u63d0\u5c31\u662f\u5f15\u5165\u4e00\u6bb5\u4ee3\u7801\u91cf\u5f88\u5c11\u7684JS\u6587\u4ef6\u5373\u53ef\u3002<\/p>\n<p>\u8be6\u7ec6\u5185\u5bb9\u8bf7\u8bbf\u95ee\u8fd9\u7bc7\u6587\u7ae0\uff1a\u201c<a href=\"https:\/\/www.zhangxinxu.com\/wordpress\/2020\/10\/css-attr-polyfill\/\">Polyfill\u540a\u70b8\u5929\u7684CSS attr()\u65b0\u8bed\u6cd5<\/a>\u201d\u3002<\/p>\n<p>\u8fd9\u91cc\u4e0d\u518d\u8d58\u8ff0\u3002<\/p>\n<h3>\u56db\u3001\u5199\u5728\u6700\u540e\u7684\u7ed3\u8bed<\/h3>\n<p>\u672c\u6587\u4ecb\u7ecd\u7684\u8fd9\u4e2a\u6280\u672f\u7684\u6838\u5fc3\u5c31\u662f\u901a\u8fc7\u8ba9\u539f\u6765\u6d4f\u89c8\u5668\u4e0d\u8ba4\u8bc6\u7684\u51fd\u6570\u6216\u8005\u8bed\u6cd5\u901a\u8fc7CSS\u53d8\u91cf\u7684\u5f62\u5f0f\u8c03\u7528\uff0c\u4ece\u800c\u8ba9\u6d4f\u89c8\u5668\u53ef\u8bc6\u522b\u3002\u5982\u679c\u6d4f\u89c8\u5668\u539f\u672c\u5c31\u8bc6\u522b\u8fd9\u4e9b\u8bed\u6cd5\uff0c\u529f\u80fd\u4e5f\u5b8c\u5168\u6b63\u5e38\uff0c\u4e0d\u53d7\u5f71\u54cd\u3002<\/p>\n<p>\u4e8e\u662f\uff0c\u6211\u4eec\u5c31\u53ef\u4ee5\u83b7\u53d6\u5230\u7528\u6237\u5bf9\u5f53\u524d\u5143\u7d20\u5177\u4f53\u8bbe\u7f6e\u7684CSS\u53d8\u91cf\u503c\uff0c\u4e5f\u5c31\u662f\u83b7\u53d6\u5230\u7528\u6237\u8bbe\u7f6e\u7684\u8fd9\u4e2a\u4e0d\u8ba4\u8bc6\u7684\u51fd\u6570\u6216\u8005\u8bed\u6cd5\u7a76\u7adf\u662f\u4ec0\u4e48\uff0c\u7136\u540e\u518d\u901a\u8fc7JavaScript\u628a\u8fd9\u4e9b\u65b0\u8bed\u6cd5\u8f6c\u6362\u6210\u6d4f\u89c8\u5668\u53ef\u8bc6\u522b\u7684\u8bed\u6cd5\u3002<\/p>\n<p>\u4e0a\u97623\u4e2a\u65b9\u6cd5\u7684\u5185\u6838\u90fd\u662f\u4e00\u6837\u7684\u3002<\/p>\n<p>\u5927\u5bb6\u9047\u5230\u7c7b\u4f3c\u7684\u9700\u6c42\u4e5f\u53ef\u4ee5\u4eff\u7167\u76f8\u5173\u7684JS\u903b\u8f91\u8fdb\u884c\u4fee\u6539\u3002<\/p>\n<p>\u53e6\u5916\uff0c\u76f8\u5173\u6280\u672f\u662f\u6211\u6700\u8fd1\u624d\u7814\u7a76\u6298\u817e\u51fa\u6765\u7684\uff0cJS\u6587\u4ef6\u53ef\u80fd\u4f1a\u6709\u7455\u75b5\uff0c\u6216\u8005bug\uff0c\u6b22\u8fce\u53cd\u9988\uff0c\u5f53\u7136\uff0c\u6211\u4e5f\u4f1a\u79ef\u6781\u5728\u751f\u6210\u73af\u5883\u4f7f\u7528\uff0c\u770b\u770b\u4f1a\u4e0d\u4f1a\u6709\u4ec0\u4e48\u95ee\u9898\uff0c\u6211\u4e5f\u4f1a\u53ca\u65f6\u66f4\u65b0\u3002<\/p>\n<p>\u5982\u679c\u4f60\u6709\u5176\u4ed6\u66f4\u597d\u7684\u5b9e\u73b0\u65b9\u6cd5\uff0c\u4e5f\u6b22\u8fce\u4ea4\u6d41\uff0c\u4e0d\u541d\u8d50\u6559\u3002<\/p>\n<p>\u6700\u540e\uff0c\u611f\u8c22\u60a8\u7684\u9605\u8bfb\uff0c\u5e0c\u671b\u672c\u6587\u7684\u5185\u5bb9\u53ef\u4ee5\u5bf9\u60a8\u7684\u5b66\u4e60\u6709\u6240\u5e2e\u52a9\u3002<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" src=\"\/\/image.zhangxinxu.com\/image\/emtion\/emoji\/2764.svg\" width=\"20\" height=\"20\" align=\"absmiddle\"><\/p>\n<p>\u672c\u6587\u4e3a\u539f\u521b\u6587\u7ae0\uff0c\u6b22\u8fce\u5206\u4eab\uff0c\u52ff\u5168\u6587\u8f6c\u8f7d\uff0c\u5982\u679c\u5b9e\u5728\u559c\u6b22\uff0c\u53ef\u6536\u85cf\uff0c\u6c38\u4e0d\u8fc7\u671f\uff0c\u4e14\u4f1a\u53ca\u65f6\u66f4\u65b0\u77e5\u8bc6\u70b9\u53ca\u4fee\u6b63\u9519\u8bef\uff0c\u9605\u8bfb\u4f53\u9a8c\u4e5f\u66f4\u597d\u3002<br \/>\n\u672c\u6587\u5730\u5740\uff1a<a href=\"https:\/\/www.zhangxinxu.com\/wordpress\/?p=9537\">https:\/\/www.zhangxinxu.com\/wordpress\/?p=9537<\/a><\/p>\n<p>\uff08\u672c\u7bc7\u5b8c\uff09<\/p>\n","protected":false},"excerpt":{"rendered":"<p><img decoding=\"async\" src=\"https:\/\/image.zhangxinxu.com\/image\/blog\/202010\/css-var-polyfill-cover.png\" width=\"160\" height=\"101\" alt=\"CSS\u53d8\u91cf\u7684\u81ea\u5b9a\u4e49\u8bed\u6cd5\u6280\u672f\" class=\"alignright size-medium\" loading=\"lazy\" \/><\/p>\n<p>\u672c\u6587\u4ecb\u7ecd\u4e00\u79cd\u81ea\u5b9a\u4e49\u4efb\u610fCSS\u8bed\u6cd5\u3001\u4ee5\u53caPolyfill CSS\u65b0\u7279\u6027\u7684\u6280\u672f\uff0c\u8fd9\u4e2a\u6280\u672f\u7684\u7cbe\u534e\u4e4b\u5904\u5c31\u662f\u5de7\u7528CSS\u53d8\u91cfvar\u8ba9\u8fd9\u4e9b\u6d4f\u89c8\u5668\u672c\u4e0d\u5e94\u8be5\u8bc6\u522b\u7684\u8bed\u6cd5\u4e5f\u8ba4\u4e3a\u662f\u5408\u6cd5\u7684\uff0c\u4ece\u800c\u8ba9JS\u8fdb\u884c\u89e3\u6790\u8bc6\u522b\u4e0e\u8f6c\u5316\u6210\u4e3a\u4e86\u53ef\u80fd\uff0c\u672c\u6587\u901a\u8fc73\u4e2a\u6848\u4f8b\u6f14\u793a\u8be5\u6280\u672f\uff0c\u5e0c\u671b\u53ef\u4ee5\u5bf9\u60a8\u7684\u5b66\u4e60\u6709\u6240\u5e2e\u52a9\u3002<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[3,180],"tags":[1518,1673,1561,1506,1674,1672,1670,1111,1052,986,1671],"_links":{"self":[{"href":"https:\/\/www.zhangxinxu.com\/wordpress\/wp-json\/wp\/v2\/posts\/9537"}],"collection":[{"href":"https:\/\/www.zhangxinxu.com\/wordpress\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.zhangxinxu.com\/wordpress\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.zhangxinxu.com\/wordpress\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.zhangxinxu.com\/wordpress\/wp-json\/wp\/v2\/comments?post=9537"}],"version-history":[{"count":0,"href":"https:\/\/www.zhangxinxu.com\/wordpress\/wp-json\/wp\/v2\/posts\/9537\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.zhangxinxu.com\/wordpress\/wp-json\/wp\/v2\/media?parent=9537"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.zhangxinxu.com\/wordpress\/wp-json\/wp\/v2\/categories?post=9537"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.zhangxinxu.com\/wordpress\/wp-json\/wp\/v2\/tags?post=9537"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}