{"id":11377,"date":"2024-10-10T23:10:38","date_gmt":"2024-10-10T15:10:38","guid":{"rendered":"https:\/\/www.zhangxinxu.com\/wordpress\/?p=11377"},"modified":"2024-10-14T10:20:45","modified_gmt":"2024-10-14T02:20:45","slug":"css-at-perporty-rule","status":"publish","type":"post","link":"https:\/\/www.zhangxinxu.com\/wordpress\/2024\/10\/css-at-perporty-rule\/","title":{"rendered":"Nice! Safari\u4e5f\u652f\u6301CSS @property\u89c4\u5219\u4e86"},"content":{"rendered":"<p>by <a href=\"https:\/\/www.zhangxinxu.com\/\">zhangxinxu<\/a> from <a href=\"https:\/\/www.zhangxinxu.com\/wordpress\/?p=11377\">https:\/\/www.zhangxinxu.com\/wordpress\/?p=11377<\/a><br \/>\n\u672c\u6587\u53ef\u5168\u6587\u8f6c\u8f7d\uff0c\u72ec\u7acb\u57df\u540d\u4e2a\u4eba\u7f51\u7ad9\u65e0\u9700\u6388\u6743\uff0c\u4f46\u9700\u8981\u4fdd\u7559\u539f\u4f5c\u8005\u3001\u51fa\u5904\u4ee5\u53ca\u6587\u4e2d\u94fe\u63a5\uff0c\u4efb\u4f55\u7f51\u7ad9\u5747\u53ef\u6458\u8981\u805a\u5408\uff0c\u5546\u7528\u8bf7\u8054\u7cfb\u6388\u6743\u3002<\/p>\n<p><script>window.wxShareImgUrl = 'https:\/\/www.zhangxinxu.com\/study\/image\/share\/202410-at-property.jpg';<\/script><\/p>\n<p><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/image.zhangxinxu.com\/image\/blog\/202410\/property-cover.jpg\" width=\"416\" height=\"269\" alt=\"\u5360\u4f4d\u56fe \u661f\u7a7a\" class=\"alignright clip thumb size-medium\" \/><\/p>\n<h3>\u4e00\u3001\u59d7\u59d7\u6765\u8fdf\u7684\u652f\u6301<\/h3>\n<p>\u5148\u770b\u517c\u5bb9\u6027\u56fe\u8868?\uff1a<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/image.zhangxinxu.com\/image\/blog\/202410\/2024-10-10_104814.png\" width=\"630\" height=\"326\" alt=\"@property\u517c\u5bb9\u6027\" class=\"alignnone size-medium\" \/><\/p>\n<p>\u53ef\u4ee5\u770b\u5230\uff0c@property\u89c4\u5219Chrome\u57282020\u5e74\u7684\u65f6\u5019\u5c31\u652f\u6301\u4e86\uff0c\u800cSafari\u6d4f\u89c8\u5668\u59d7\u59d7\u6765\u8fdf\uff0c2023\u5e74\u624d\u652f\u6301\uff0c\u76ee\u524d\u5df2\u7ecf\u8fdb\u5165\u53ef\u4ee5\u518d\u751f\u4ea7\u73af\u5883\u4f7f\u7528\u7684\u9636\u6bb5\uff08\u5bf9\u517c\u5bb9\u6027\u8981\u6c42\u4e0d\u662f\u90a3\u4e48\u4e25\u82db\u7684\u9879\u76ee\uff09\u3002<\/p>\n<p>@property\u89c4\u5219\u5c5e\u4e8eCSS Houdini\u4e2d\u7684\u4e00\u4e2a\u7279\u6027\uff0c\u53ef\u4ee5\u81ea\u5b9a\u4e49CSS\u5c5e\u6027\u7684\u7c7b\u578b\uff0c\u8fd9\u4e2a\u7279\u6027\u5728\u73b0\u4ee3CSS\u5f00\u53d1\u4e2d\u8fd8\u662f\u5f88\u6709\u7528\u7684\uff0c\u6700\u5177\u4ee3\u8868\u6027\u7684\u4f8b\u5b50\u5c31\u662f\u53ef\u4ee5\u8ba9CSS\u53d8\u91cf\u652f\u6301\u52a8\u753b\u6216\u8fc7\u6e21\u6548\u679c\u3002<\/p>\n<h3>\u4e8c\u3001\u201c\u53d8\u91cf\u79cd\u5b50\u8ba1\u6570\u5668\u201d\u7684\u5347\u7ea7<\/h3>\n<p>\u572819\u5e74\u7684\u65f6\u5019\uff0c\u6211\u66fe\u63d0\u8fc7\u4f7f\u7528\u201c\u53d8\u91cf\u79cd\u5b50\u8ba1\u6570\u5668\u201d\u6765\u98a0\u8986\u73b0\u6709\u7684CSS\u52a8\u753b\u5b9e\u73b0\u65b9\u5f0f\u3002<\/p>\n<p>\u4f20\u7edf\u7684CSS\u52a8\u753b\u90fd\u662f\u4e00\u4e2a\u52a8\u753b\u5b9a\u4e49\u4e00\u4e2a @keyframes\uff0c\u4f46\u5982\u679c\u52a8\u753b\u7c7b\u578b\u591a\uff0c\u90a3\u9875\u9762\u5c31\u4f1a\u6709\u591a\u4e2a\u4e0d\u540c\u7c7b\u578b\u7684@keyframes\uff0c\u4ee3\u7801\u5c31\u4f1a\u5f88\u5570\u55e6\u3002<\/p>\n<p>\u800c\u4f7f\u7528\u8fd9\u91cc\u7684\u201c\u53d8\u91cf\u79cd\u5b50\u8ba1\u6570\u5668\u201d\uff0c\u6240\u6709\u7684\u52a8\u753b\u90fd\u53ef\u4ee5\u4f7f\u7528\u552f\u4e00\u7684\u4e00\u4e2a@keyframes\u52a8\u753b\u89c4\u5219\u89e3\u51b3\u3002<\/p>\n<p>\u8be6\u89c1\u6587\u7ae0\uff1a\u201c<a href=\"https:\/\/www.zhangxinxu.com\/wordpress\/2019\/05\/css-variable-seed-extend-animation\/\">\u4f7f\u7528\u2018\u53d8\u91cf\u79cd\u5b50\u8ba1\u6570\u5668\u2019\u6269\u5c55CSS\u52a8\u753b\u66f4\u591a\u53ef\u80fd\u6027<\/a>\u201d<\/p>\n<p>\u4f46\u662f\uff0c\u4e4b\u524d\u53d7\u5236\u4e8e\u6d4f\u89c8\u5668\u7684\u663e\u793a\uff0c\u5176@keyframes\u89c4\u5219\u662f\u4f7f\u7528\u679a\u4e3e\u7684\u65b9\u5f0f\u5b9e\u73b0\u7684\uff0c\u4f8b\u5982\uff08\u4e2d\u95f4\u90e8\u5206\u4ee3\u7801\u7701\u7565\uff09\uff1a<\/p>\n<pre>@keyframes seed {\r\n    0%{--seed:0}1%{--seed:1}2%{--seed:2}3%{--seed:3}4%{--seed:4}5%{--seed:5}6%{--seed:6}7%{--seed:7}8%{--seed:8}9%{--seed:9}10%{--seed:10}11%{--seed:11}...95%{--seed:95}96%{--seed:96}97%{--seed:97}98%{--seed:98}99%{--seed:99}100%{--seed:100}\r\n}<\/pre>\n<p>\u6bd4\u65b9\u8bf4\u8981\u5b9e\u73b0\u6de1\u51fa\u52a8\u753b\u6548\u679c\uff0c\u5c31\u662f\u8fd9\u6837\uff1a<\/p>\n<pre>.element {\r\n  animation: seed .2s both;\r\n  opacity: calc(var(--seed) \/ 100);\r\n}<\/pre>\n<p>\u5b9e\u73b0loading\u56fe\u6807\u4e0d\u505c\u65cb\u8f6c\uff0c\u5c31\u662f\u8fd9\u6837\uff1a<\/p>\n<pre>.spin {\r\n  animation: seed 1s linear infinite;\r\n  transform: rotate(calc(var(--seed) * 3.6deg));\r\n}<\/pre>\n<p>\u6240\u6709\u52a8\u753b\u90fd\u4f7f\u7528seed\u8fd9\u4e2a\u52a8\u753b\uff0c\u800c\u53d8\u5316\u7684\u6837\u5f0f\u5c31\u4f7f\u7528var(&#8211;seed)\u8fdb\u884c\u8ba1\u7b97\u5373\u53ef\uff0c\u53ef\u4ee5\u5927\u5927\u7b80\u5316CSS\u52a8\u753b\u5b9e\u73b0\u7684\u4e66\u5199\u3002<\/p>\n<p>\u5f88\u68d2\u7684\u521b\u610f\uff0c\u5bf9\u4e0d\u5bf9\uff1f<\/p>\n<p>\u4f46\u662f\uff0c\u6709\u4e2a\u4e0d\u8db3\uff0c<code>@keyframes seed {}<\/code>\u91cc\u9762\u7684\u7684\u4ee3\u7801\u662f\u679a\u4e3e\u5b9e\u73b0\u7684\uff0c\u5f88\u5570\u55e6\uff0c\u4ee3\u7801\u4e0d\u4f18\u96c5\u3002<\/p>\n<p>\u800c\u6709\u4e2a@property\u89c4\u5219\uff0c\u8fd9\u4e2a\u4e0d\u8db3\u5c31\u4e0d\u518d\u662f\u95ee\u9898\u4e86\u3002<\/p>\n<h4>@property\u8ba9\u53d8\u91cf\u652f\u6301\u52a8\u753b<\/h4>\n<p>\u5b9e\u73b0\u65b9\u6cd5\u5982\u4e0b\uff1a<\/p>\n<ul>\n<li>\u4f7f\u7528@property\u89c4\u5219\u5b9a\u4e49<code>--seed<\/code>\u53d8\u91cf\u4e3a\u6570\u503c\u7c7b\u578b<\/li>\n<\/ul>\n<p>\u7136\u540e\uff0c\u5c31\u6ca1\u6709\u7136\u540e\u4e86\uff0c\u4e00\u6b65\u5230\u4f4d\uff0c\u5c31\u662f\u8fd9\u4e48\u7b80\u5355\uff0c\u6b64\u65f6&#8211;seed\u5c31\u53ef\u4ee5\u50cf\u666e\u901a\u7684\u5c5e\u6027\u90a3\u6837\u652f\u63010%-100%\u7684\u52a8\u753b\u8ba1\u7b97\u4e86\u3002<\/p>\n<p>\u4ee3\u7801\u793a\u610f\uff1a<\/p>\n<pre>@property --seed {\r\n  syntax: \"&lt;integer&gt;\";\r\n  inherits: true;\r\n  initial-value: 0;\r\n}\r\n\r\n@keyframes seed {\r\n  from { --seed: 0; }\r\n  to { --seed: 100; }\r\n}<\/pre>\n<p>\u6b64\u65f6\uff0c\u50cf\u4e0a\u9762\u7684\u52a8\u753bCSS\u4ee3\u7801\uff0c\u8be5\u6de1\u51fa\u7684\u5c31\u6de1\u51fa\uff0c\u8be5\u65cb\u8f6c\u7684\u5c31\u65cb\u8f6c\uff1a<\/p>\n<pre>.element {\r\n  animation: seed .2s both;\r\n  opacity: calc(var(--seed) \/ 100);\r\n}\r\n\r\n.spin {\r\n  animation: seed 1s linear infinite;\r\n  transform: rotate(calc(var(--seed) * 3.6deg));\r\n}<\/pre>\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\/202410\/css-@property-animation-seed-demo.php\" rel=\"noopener\" target=\"_blank\">@property\u4e0e\u53d8\u91cf\u79cd\u5b50\u52a8\u753b\u8ba1\u6570\u5668\u6548\u679cdemo<\/a><\/p>\n<p><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/image.zhangxinxu.com\/image\/blog\/202410\/2024-10-10_225555.jpg\" width=\"451\" height=\"377\" alt=\"demo\u6548\u679c\u622a\u56fe\" class=\"alignnone size-medium\" \/><\/p>\n<h3>\u4e09\u3001@property\u89c4\u5219\u7684\u8bed\u6cd5<\/h3>\n<p>\u6700\u540e\uff0c\u4e86\u89e3\u4e0b<code>@property<\/code>\u89c4\u5219\u7684\u8bed\u6cd5\uff0c\u5982\u4e0b\u6240\u793a\uff1a<\/p>\n<pre>@property --property-name {\r\n  syntax: \"&lt;color&gt;\";\r\n  inherits: false;\r\n  initial-value: #c0ffee;\r\n}<\/pre>\n<p>\u5176\u4e2d\uff0c<code>inherits<\/code>\u8868\u793a\u7ee7\u627f\u4e0e\u5426\uff0c\u503c\u4e3atrue\u6216false\uff0c<code>initial-value<\/code>\u662f\u521d\u59cb\u503c\uff0c\u8fd9\u4e2a\u4e5f\u5f88\u597d\u7406\u89e3\u3002<\/p>\n<p>\u6700\u9700\u8981\u5173\u6ce8\u7684\u5c31\u662f\u8fd9\u91cc\u7684<code>syntax<\/code>\uff0c\u8868\u793a\u53d8\u91cf\u7684\u7c7b\u578b\uff0c\u5230\u5e95\u662f\u6570\u503c\u5462\uff0c\u989c\u8272\u5462\uff0c\u8fd8\u662f\u767e\u5206\u6bd4\u7b49\u3002<\/p>\n<p>\u652f\u6301\u7684\u503c\u5305\u62ec\u8fd9\u4e9b\uff1a<\/p>\n<p><code>&lt;length&gt;<\/code>, <code>&lt;number&gt;<\/code>, <code>&lt;percentage&gt;<\/code>, <code>&lt;length-percentage&gt;<\/code>, <code>&lt;color&gt;<\/code>, <code>&lt;image&gt;<\/code>, <code>&lt;url&gt;<\/code>, <code>&lt;integer&gt;<\/code>, <code>&lt;angle&gt;<\/code>, <code>&lt;time&gt;<\/code>, <code>&lt;resolution&gt;<\/code>, <code>&lt;transform-function&gt;<\/code> \u6216\u8005 <code>&lt;custom-ident&gt;<\/code>\uff0c\u4e5f\u652f\u6301\u591a\u4e2a\u7c7b\u578b\u5e76\u5217\uff0c\u4e0d\u8fc7\u8fd9\u4e2a\u65e5\u5e38\u5f00\u53d1\u7528\u4e0d\u5230\uff0c\u5927\u5bb6\u65e0\u9700\u5728\u610f\u3002<\/p>\n<h4>\u4f7f\u7528JS\u6ce8\u518cCSS\u5c5e\u6027<\/h4>\n<p>CSS\u5c5e\u6027\u7684\u81ea\u5b9a\u4e49\u8bbe\u7f6e\u9664\u4e86\u4f7f\u7528<code>@property<\/code>\u89c4\u5219\uff0c\u8fd8\u53ef\u4ee5\u4f7f\u7528\u5168\u5c40CSS\u5bf9\u8c61\u7684<code>registerProperty<\/code>\u65b9\u6cd5\u5b8c\u6210\uff0c\u4f8b\u5982\uff1a<\/p>\n<pre>window.CSS.registerProperty({\r\n  name: \"--item-color\",\r\n  syntax: \"<color>\",\r\n  inherits: false,\r\n  initialValue: \"aqua\",\r\n});<\/pre>\n<p>\u5b9e\u9645\u5f00\u53d1\u7684\u65f6\u5019\uff0c<code>window.<\/code>\u662f\u53ef\u4ee5\u7701\u7565\u7684\uff0c\u8fd9\u91cc\u53ea\u662f\u4fbf\u4e8e\u5927\u5bb6\u5b66\u4e60\u66b4\u9732\u51fa\u6765\u7684\u3002<\/p>\n<h3>\u56db\u3001\u5176\u4ed6\u4e9b\u5e94\u7528\u573a\u666f<\/h3>\n<p><code>@property<\/code>\u89c4\u5219\u8fd8\u6709\u4e2a\u7ecf\u5178\u7684\u5e94\u7528\u573a\u666f\u5c31\u662f\u4ece\u6e10\u53d8A\u5230\u6e10\u53d8B\u7684\u8fc7\u6e21\u6548\u679c\u5b9e\u73b0\u3002<\/p>\n<p>\u9ed8\u8ba4\u60c5\u51b5\u4e0b\uff0c\u4f7f\u7528background\u6e10\u53d8\u5b9e\u73b0\u7684\u80cc\u666f\u672c\u8d28\u4e0a\u662f\u56fe\u50cf\uff0c\u662f\u65e0\u6cd5\u5e94\u7528transition\u8fc7\u6e21\u6548\u679c\u7684\u3002<\/p>\n<p>\u4f46\u5982\u679c\u5c06\u6e10\u53d8\u80cc\u666f\u7684\u989c\u8272\u63d0\u53d6\u6210CSS\u53d8\u91cf\uff0c\u5e76\u4f7f\u7528<code>@property<\/code>\u89c4\u5219\u91cd\u65b0\u5b9a\u4e49\uff0c\u90a3\u8fc7\u6e21\u6548\u679c\u5c31\u6709\u4e86\u3002<\/p>\n<p>\u6848\u4f8b\uff0c\u5df2\u77e5HTML\u5982\u4e0b\uff1a<\/p>\n<pre>&lt;canvas&gt;&lt;\/canvas&gt;<\/pre>\n<p><span class=\"s\">\/\/zxx: canvas\u5143\u7d20\u81ea\u5e26\u5bbd\u9ad8\uff0c\u7528\u6765\u793a\u610f\u4ee3\u7801\u6700\u4e3a\u7b80\u6d01\u3002<\/span><\/p>\n<p>CSS\u4ee3\u7801\u5982\u4e0b\uff1a<\/p>\n<pre>@property --end-color {\r\n  syntax: \"&lt;color&gt;\";\r\n  inherits: false;\r\n  initial-value: black;\r\n}\r\ncanvas {\r\n  background: linear-gradient(skyblue, var(--end-color));\r\n  transition: --end-color .2s;\r\n}\r\ncanvas:hover {\r\n  --end-color: pink;\r\n}<\/pre>\n<p>\u6b64\u65f6\uff0chover\u60ac\u505ccanvas\u5143\u7d20\u7684\u65f6\u5019\uff0c\u5c31\u53ef\u4ee5\u770b\u5230\u6e10\u53d8\u80cc\u666f\u7684\u8fc7\u6e21\u52a8\u753b\u6548\u679c\u4e86\uff0c\u5982\u4e0bGIF\u5f55\u5c4f\u6240\u793a\u3002<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/image.zhangxinxu.com\/image\/blog\/202410\/gradient-hover.gif\" width=\"312\" height=\"160\" alt=\"\u6e10\u53d8\u80cc\u666f\u8fc7\u6e21\u52a8\u753b\" class=\"alignnone size-medium\" \/><\/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\/202410\/css-@property-gradient-transition-demo.php\" rel=\"noopener\" target=\"_blank\">@property\u4e0e\u6e10\u53d8\u80cc\u666f\u8fc7\u6e21\u52a8\u753b\u6548\u679cdemo<\/a><\/p>\n<p>\u597d\u5427\uff0c\u5dee\u4e0d\u591a\u5c31\u8fd9\u4e9b\u5185\u5bb9\u3002<\/p>\n<p>\u611f\u8c22\u9605\u8bfb\uff0c\u611f\u8c22<button onclick=\"shareWeixin.click()\" class=\"entry-share-btn\">\u5206\u4eab<\/button>\u3002<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/image.zhangxinxu.com\/image\/blog\/202410\/2024-10-10_230235.jpg\" width=\"120\" height=\"131\" alt=\"\u51cc\u7389\u7075AI\u56fe\" class=\"alignnone size-medium\" \/><\/p>\n<p>\u672c\u6587\u4e3a\u539f\u521b\u6587\u7ae0\uff0c\u4f1a\u7ecf\u5e38\u66f4\u65b0\u77e5\u8bc6\u70b9\u4ee5\u53ca\u4fee\u6b63\u4e00\u4e9b\u9519\u8bef\uff0c\u56e0\u6b64\u8f6c\u8f7d\u8bf7\u4fdd\u7559\u539f\u51fa\u5904\uff0c\u65b9\u4fbf\u6eaf\u6e90\uff0c\u907f\u514d\u9648\u65e7\u9519\u8bef\u77e5\u8bc6\u7684\u8bef\u5bfc\uff0c\u540c\u65f6\u6709\u66f4\u597d\u7684\u9605\u8bfb\u4f53\u9a8c\u3002<br \/>\n\u672c\u6587\u5730\u5740\uff1a<a href=\"https:\/\/www.zhangxinxu.com\/wordpress\/?p=11377\">https:\/\/www.zhangxinxu.com\/wordpress\/?p=11377<\/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\/202410\/property-cover.jpg\" width=\"160\" height=\"103\" alt=\"\u5360\u4f4d\u56fe \u661f\u7a7a\" class=\"alignright size-medium\" loading=\"lazy\" \/><\/p>\n<p>CSS @property\u89c4\u5219\u6700\u5177\u4ee3\u8868\u6027\u7684\u4f5c\u7528\u662f\u6269\u5927\u4e86CSS\u52a8\u753b\u548cCSS\u8fc7\u6e21\u6548\u679c\u7684\u5e94\u7528\u8303\u56f4\uff0c\u8fd8\u662f\u633a\u5b9e\u7528\u7684\u4e00\u4e2aHoudini\u7279\u6027\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],"tags":[1013,1926,1596,1369,204,289],"_links":{"self":[{"href":"https:\/\/www.zhangxinxu.com\/wordpress\/wp-json\/wp\/v2\/posts\/11377"}],"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=11377"}],"version-history":[{"count":0,"href":"https:\/\/www.zhangxinxu.com\/wordpress\/wp-json\/wp\/v2\/posts\/11377\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.zhangxinxu.com\/wordpress\/wp-json\/wp\/v2\/media?parent=11377"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.zhangxinxu.com\/wordpress\/wp-json\/wp\/v2\/categories?post=11377"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.zhangxinxu.com\/wordpress\/wp-json\/wp\/v2\/tags?post=11377"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}