{"id":52059,"date":"2024-12-03T17:05:59","date_gmt":"2024-12-03T09:05:59","guid":{"rendered":"https:\/\/fwq.ai\/blog\/52059\/"},"modified":"2024-12-03T17:05:59","modified_gmt":"2024-12-03T09:05:59","slug":"css-%e6%96%b0%e7%9a%84-position-try-%e7%9a%84%e4%be%bf%e5%88%a9%e6%80%a7","status":"publish","type":"post","link":"https:\/\/fwq.ai\/blog\/52059\/","title":{"rendered":"CSS \u65b0\u7684 @position-try \u7684\u4fbf\u5229\u6027"},"content":{"rendered":"<p><b><\/b>     <\/p>\n<h1>CSS \u65b0\u7684 @position-try \u7684\u4fbf\u5229\u6027<\/h1>\n<p><span style=\"cursor: pointer\"><i><\/i>\u6536\u85cf<\/span>    <\/p>\n<p>\u4eca\u5929\u7c73\u4e91\u7ed9\u5927\u5bb6\u5e26\u6765\u4e86<span style=\"color: #FF6600;, Helvetica, Arial, sans-serif;font-size: 14px;background-color: #FFFFFF\">\u300aCSS \u65b0\u7684 @position-try \u7684\u4fbf\u5229\u6027\u300b<\/span>\uff0c\u5176\u4e2d\u6d89\u53ca\u5230\u7684\u77e5\u8bc6\u70b9\u5305\u62ec<span style=\"color: #FF6600;, Helvetica, Arial, sans-serif;font-size: 14px;background-color: #FFFFFF\"><\/span>\u7b49\u7b49\uff0c\u65e0\u8bba\u4f60\u662f\u5c0f\u767d\u8fd8\u662f\u8001\u624b\uff0c\u90fd\u9002\u5408\u770b\u4e00\u770b\u54e6~\u6709\u597d\u7684\u5efa\u8bae\u4e5f\u6b22\u8fce\u5927\u5bb6\u5728\u8bc4\u8bba\u7559\u8a00\uff0c\u82e5\u662f\u770b\u5b8c\u6709\u6240\u6536\u83b7\uff0c\u4e5f\u5e0c\u671b\u5927\u5bb6\u80fd\u591a\u591a\u70b9\u8d5e\u652f\u6301\u5440\uff01\u4e00\u8d77\u52a0\u6cb9\u5b66\u4e60~<\/p>\n<p>css\u951a\u70b9\u5b9a\u4f4d\u524d\u6bb5\u65f6\u95f4\u5c31\u51fa\u6765\u4e86\u3002\u5982\u679c\u4f60\u548c\u6211\u4e00\u6837\u4e0d\u559c\u6b22\u7f16\u5199 css\uff0c\u8fd9\u4e2a\u65b0\u7684 api \u80af\u5b9a\u4f1a\u6709\u6240\u6539\u5584\u3002<\/p>\n<h2> \u951a\u5b9a\u4f4d <\/h2>\n<p>css \u951a\u70b9\u5b9a\u4f4d api \u5141\u8bb8\u5f00\u53d1\u4eba\u5458\u8f7b\u677e\u76f8\u5bf9\u4e8e\u5176\u4ed6\u5143\u7d20\uff08\u79f0\u4e3a\u951a\u70b9\uff09\u5b9a\u4f4d\u5143\u7d20\uff0c\u65e0\u9700\u989d\u5916\u7684\u5e93\u6216\u590d\u6742\u7684 javascript\u3002\u6b64\u529f\u80fd\u975e\u5e38\u9002\u5408\u521b\u5efa\u83dc\u5355\u3001\u5de5\u5177\u63d0\u793a\u3001\u5bf9\u8bdd\u6846\u548c\u5176\u4ed6\u5206\u5c42\u754c\u9762\u3002<\/p>\n<p>\u4f7f\u7528\u6b64 api\uff0c\u60a8\u53ef\u4ee5\u786e\u4fdd\u5143\u7d20\u7684\u5927\u5c0f\/\u4f4d\u7f6e\u6839\u636e\u5176\u951a\u70b9\u8fdb\u884c\u8c03\u6574\u3002\u8fd9\u6d88\u9664\u4e86\u624b\u52a8\u8c03\u6574\u7684\u9700\u8981\uff0c\u5e76\u5728\u6784\u5efa\u52a8\u6001\u3001\u4ea4\u4e92\u5f0f\u5e03\u5c40\u65f6\u63d0\u4f9b\u66f4\u6d41\u7545\u3001\u54cd\u5e94\u66f4\u7075\u654f\u7684\u4f53\u9a8c\u3002<\/p>\n<h2> \u4ec0\u4e48\u662f\u4f4d\u7f6e\u5c1d\u8bd5\uff1f <\/h2>\n<p>anchor css \u8fd8\u63a8\u51fa\u4e86\u4e00\u4e2a\u65b0\u7684 css at-rule\uff0c\u79f0\u4e3a <strong>position-try<\/strong>\u3002 \u5f53\u951a\u5b9a\u5143\u7d20\u4e0d\u9002\u5408\u5c4f\u5e55\u6216\u5bb9\u5668\u65f6\uff0c\u5b83\u5141\u8bb8\u60a8\u5b9a\u4e49\u951a\u5b9a\u5143\u7d20\u7684\u540e\u5907\u4f4d\u7f6e\u3002\u5982\u679c\u67d0\u4e2a\u5143\u7d20\u6ea2\u51fa\uff0c\u6d4f\u89c8\u5668\u4f1a\u81ea\u52a8\u9009\u62e9\u4e0b\u4e00\u4e2a\u66ff\u4ee3\u4f4d\u7f6e\uff0c\u786e\u4fdd\u5b83\u4fdd\u6301\u5b8c\u5168\u53ef\u89c1\u548c\u529f\u80fd\u6b63\u5e38\u3002 <strong>position-try-fallbacks<\/strong> \u5c5e\u6027\u53ef\u7528\u4e8e\u5b9a\u4e49\u6d4f\u89c8\u5668 <strong>try<\/strong> \u7684\u591a\u4e2a\u540e\u5907\u4f4d\u7f6e\u3002\u65e9\u4e9b\u65f6\u5019\uff0c\u8fd9\u53ef\u4ee5\u901a\u8fc7\u8fd0\u884c\u4e00\u4e2a\u4fa6\u542c\u5668\u6765\u5b9e\u73b0\uff0c\u8be5\u4fa6\u542c\u5668\u5728\u6bcf\u6b21\u89c6\u200b\u200b\u53e3\u66f4\u6539\u540e\u68c0\u67e5\u4e8b\u60c5\u662f\u5426\u8fdb\u5c55\u987a\u5229\u3002 <\/p>\n<p>\u8fd9\u53ef\u4ee5\u907f\u514d\u5728\u4f7f\u7528\u4e0b\u62c9\u83dc\u5355\u3001\u5de5\u5177\u63d0\u793a\u7b49\u65f6\u51fa\u73b0\u5f88\u591a\u4ee4\u4eba\u5934\u75bc\u7684\u95ee\u9898\uff0c\u56e0\u4e3a\u73b0\u5728\u6211\u4eec\u4e0d\u5fc5\u7f16\u5199\u81ea\u5b9a\u4e49\u903b\u8f91\u6765\u68c0\u67e5\u6ea2\u51fa\u60c5\u51b5\u3002<\/p>\n<h2> \u6f14\u793a <\/h2>\n<p>\u8fd9\u662f\u6211\u4f7f\u7528\u4e0a\u8ff0 css \u5c5e\u6027\u7f16\u5199\u7684\u4ee3\u7801\u7684\u5feb\u901f\u6f14\u793a\uff1a<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/www.17golang.com\/uploads\/20241118\/1731935575673b3d578c3d2.jpg\" class=\"aligncenter\" title=\"CSS \u65b0\u7684 @position-try \u7684\u4fbf\u5229\u6027\u63d2\u56fe\" alt=\"CSS \u65b0\u7684 @position-try \u7684\u4fbf\u5229\u6027\u63d2\u56fe\" \/><\/p>\n<p>\u6211\u90a3\u4e11\u964b\u7684\u5bfc\u822a\u680f\u4e2d\u7684\u5b50\u83dc\u5355\u4f1a\u6839\u636e\u89c6\u53e3\u7684\u5bbd\u5ea6\u6539\u53d8\u5176\u4f4d\u7f6e\u3002<\/p>\n<p>\u4ee3\u7801\u662f\u7528react\u7f16\u5199\u7684\u3002\u65e9\u4e9b\u65f6\u5019\u6211\u5fc5\u987b\u4f7f\u7528\u6548\u679c\u6765\u505a\u5230\u8fd9\u4e00\u70b9\u3002\u5728\u6211\u7684\u6548\u679c\u4e2d\uff0c\u6211\u4f1a\u68c0\u67e5\u5b50\u83dc\u5355\u5143\u7d20\u662f\u5426\u8de8\u8d8a\u89c6\u53e3\u7684\u8fb9\u754c\u3002\u5982\u679c\u662f\uff0c\u6211\u4f1a\u89e6\u53d1\u53e6\u4e00\u4e2a\u91cd\u65b0\u6e32\u67d3\u6765\u66f4\u65b0\u5b50\u83dc\u5355\u7684\u6837\u5f0f\u3002\u7531\u4e8e useeffect \u5728\u7ed8\u5236\u4e4b\u540e\u8fd0\u884c\uff0c\u5e76\u4e14\u6211\u4eec\u4e0d\u5e0c\u671b\u7528\u6237\u5728\u9519\u8bef\u7684\u4f4d\u7f6e\u770b\u5230\u5b50\u83dc\u5355\uff0c\u56e0\u6b64\u6211\u5fc5\u987b\u4e3a\u6b64\u4f7f\u7528 uselayouteffect\u3002<\/p>\n<p>\u73b0\u5728\u6211\u6240\u8981\u505a\u7684\u5c31\u662f\u7f16\u5199\u8fd9\u6837\u7684 css\uff1a<\/p>\n<pre>.button-anchor {\n  anchor-name: --anchorButton;\n}\n\n@position-try --bottom {\n  position-area: bottom;\n}\n\n.menu-list {\n  position-anchor: --anchorButton;\n  border: 1px solid #000;\n  font-family: sans-serif;\n  width: 60px;\n  font-size: 12px;\n  display: flex;\n  flex-direction: column;\n  row-gap: 4px;\n  padding: 8px;\n  position: fixed;\n  position-area: right span-y-end;\n  position-try-fallbacks: --bottom;\n}\n<\/pre>\n<p>@position-try &#8211; \u521b\u5efa\u540d\u4e3a &#8211;bottom \u7684\u89c4\u5219\u3002<br \/> \u951a\u70b9\u540d\u79f0 &#8211; \u5c06\u6309\u94ae\u8bbe\u7f6e\u4e3a\u951a\u70b9\u5143\u7d20\u3002<br \/> position-anchor &#8211; \u8ba9\u83dc\u5355\u5217\u8868\u4f7f\u7528 &#8211;anchorbutton \u4f5c\u4e3a\u951a\u5143\u7d20\u3002<br \/> position-try-fallbacks &#8211; \u5e2e\u52a9\u5217\u51fa\u8981\u5c1d\u8bd5\u7684\u591a\u4e2a\u540e\u5907\u3002\u8fd9\u610f\u5473\u7740\u5373\u4f7f &#8211;bottom \u5931\u8d25\uff0c\u4e5f\u53ef\u4ee5\u6709\u66f4\u591a\u4f4d\u7f6e\u3002<\/p>\n<h2> \u6982\u62ec <\/h2>\n<p>anchor css \u7684\u51fa\u73b0\u4e3a\u5f00\u53d1\u4eba\u5458\u89e3\u51b3\u4e86\u4e00\u4e9b\u975e\u5e38\u6709\u8da3\u7684\u7528\u4f8b\u3002\u9664\u4e86\u4e0a\u8ff0\u4e4b\u5916\uff0c\u7cfb\u7559\u5b9a\u4f4d\u53d8\u5f97\u975e\u5e38\u5bb9\u6613\u3002\u4e00\u5207\u90fd\u7eaf\u7cb9\u57fa\u4e8e css\uff0c\u56e0\u6b64\u6027\u80fd\u4e5f\u5f88\u68d2\u3002 \u76ee\u524d\u6d4f\u89c8\u5668\u652f\u6301\u8fd8\u4e0d\u662f\u5f88\u597d\uff0c\u4f46\u6211\u5e0c\u671b\u5b83\u5f88\u5feb\u5c31\u4f1a\u88ab\u66f4\u591a\u4eba\u91c7\u7528\u3002<\/p>\n<p>\u672c\u7bc7\u5173\u4e8e\u300aCSS \u65b0\u7684 @position-try \u7684\u4fbf\u5229\u6027\u300b\u7684\u4ecb\u7ecd\u5c31\u5230\u6b64\u7ed3\u675f\u5566\uff0c\u4f46\u662f\u5b66\u65e0\u6b62\u5883\uff0c\u60f3\u8981\u4e86\u89e3\u5b66\u4e60\u66f4\u591a\u5173\u4e8e\u6587\u7ae0\u7684\u76f8\u5173\u77e5\u8bc6\uff0c\u8bf7\u5173\u6ce8\u7c73\u4e91\u516c\u4f17\u53f7\uff01<\/p>\n<p>      \u7248\u672c\u58f0\u660e \u672c\u6587\u8f6c\u8f7d\u4e8e\uff1adev.to \u5982\u6709\u4fb5\u72af\uff0c\u8bf7\u8054\u7cfb\u5220\u9664    <\/p>\n<dl>\n<dt><\/dt>\n<dd>\n   Vue3 \u8de8\u57df\u95ee\u9898\uff1a\u914d\u7f6e\u65e0\u6548\uff1f\u5982\u4f55\u6b63\u786e\u89e3\u51b3\uff1f\n <\/dd>\n<\/dl>\n","protected":false},"excerpt":{"rendered":"<p>CSS \u65b0\u7684 @position-try \u7684\u4fbf\u5229\u6027 \u6536\u85cf \u4eca\u5929\u7c73\u4e91\u7ed9\u5927\u5bb6\u5e26\u6765\u4e86\u300aCSS \u65b0\u7684 @position-try \u7684\u4fbf\u5229\u6027\u300b\uff0c\u5176\u4e2d\u6d89\u53ca\u5230\u7684\u77e5\u8bc6\u70b9\u5305\u62ec\u7b49\u7b49\uff0c\u65e0\u8bba\u4f60\u662f\u5c0f\u767d\u8fd8\u662f\u8001\u624b\uff0c\u90fd\u9002\u5408\u770b\u4e00\u770b\u54e6~\u6709\u597d\u7684\u5efa\u8bae\u4e5f\u6b22\u8fce\u5927\u5bb6\u5728\u8bc4\u8bba\u7559\u8a00\uff0c\u82e5\u662f\u770b\u5b8c\u6709\u6240\u6536\u83b7\uff0c\u4e5f\u5e0c\u671b\u5927\u5bb6\u80fd\u591a\u591a\u70b9\u8d5e\u652f\u6301\u5440\uff01\u4e00\u8d77\u52a0\u6cb9\u5b66\u4e60~ css\u951a\u70b9\u5b9a\u4f4d\u524d\u6bb5\u65f6\u95f4\u5c31\u51fa\u6765\u4e86\u3002\u5982\u679c\u4f60\u548c\u6211\u4e00\u6837\u4e0d\u559c\u6b22\u7f16\u5199 css\uff0c\u8fd9\u4e2a\u65b0\u7684 api \u80af\u5b9a\u4f1a\u6709\u6240\u6539\u5584\u3002 \u951a\u5b9a\u4f4d css \u951a\u70b9\u5b9a\u4f4d api \u5141\u8bb8\u5f00\u53d1\u4eba\u5458\u8f7b\u677e\u76f8\u5bf9\u4e8e\u5176\u4ed6\u5143\u7d20\uff08\u79f0\u4e3a\u951a\u70b9\uff09\u5b9a\u4f4d\u5143\u7d20\uff0c\u65e0\u9700\u989d\u5916\u7684\u5e93\u6216\u590d\u6742\u7684 javascript\u3002\u6b64\u529f\u80fd\u975e\u5e38\u9002\u5408\u521b\u5efa\u83dc\u5355\u3001\u5de5\u5177\u63d0\u793a\u3001\u5bf9\u8bdd\u6846\u548c\u5176\u4ed6\u5206\u5c42\u754c\u9762\u3002 \u4f7f\u7528\u6b64 api\uff0c\u60a8\u53ef\u4ee5\u786e\u4fdd\u5143\u7d20\u7684\u5927\u5c0f\/\u4f4d\u7f6e\u6839\u636e\u5176\u951a\u70b9\u8fdb\u884c\u8c03\u6574\u3002\u8fd9\u6d88\u9664\u4e86\u624b\u52a8\u8c03\u6574\u7684\u9700\u8981\uff0c\u5e76\u5728\u6784\u5efa\u52a8\u6001\u3001\u4ea4\u4e92\u5f0f\u5e03\u5c40\u65f6\u63d0\u4f9b\u66f4\u6d41\u7545\u3001\u54cd\u5e94\u66f4\u7075\u654f\u7684\u4f53\u9a8c\u3002 \u4ec0\u4e48\u662f\u4f4d\u7f6e\u5c1d\u8bd5\uff1f anchor css \u8fd8\u63a8\u51fa\u4e86\u4e00\u4e2a\u65b0\u7684 css at-rule\uff0c\u79f0\u4e3a position-try\u3002 \u5f53\u951a\u5b9a\u5143\u7d20\u4e0d\u9002\u5408\u5c4f\u5e55\u6216\u5bb9\u5668\u65f6\uff0c\u5b83\u5141\u8bb8\u60a8\u5b9a\u4e49\u951a\u5b9a\u5143\u7d20\u7684\u540e\u5907\u4f4d\u7f6e\u3002\u5982\u679c\u67d0\u4e2a\u5143\u7d20\u6ea2\u51fa\uff0c\u6d4f\u89c8\u5668\u4f1a\u81ea\u52a8\u9009\u62e9\u4e0b\u4e00\u4e2a\u66ff\u4ee3\u4f4d\u7f6e\uff0c\u786e\u4fdd\u5b83\u4fdd\u6301\u5b8c\u5168\u53ef\u89c1\u548c\u529f\u80fd\u6b63\u5e38\u3002 position-try-fallbacks \u5c5e\u6027\u53ef\u7528\u4e8e\u5b9a\u4e49\u6d4f\u89c8\u5668 try \u7684\u591a\u4e2a\u540e\u5907\u4f4d\u7f6e\u3002\u65e9\u4e9b\u65f6\u5019\uff0c\u8fd9\u53ef\u4ee5\u901a\u8fc7\u8fd0\u884c\u4e00\u4e2a\u4fa6\u542c\u5668\u6765\u5b9e\u73b0\uff0c\u8be5\u4fa6\u542c\u5668\u5728\u6bcf\u6b21\u89c6\u200b\u200b\u53e3\u66f4\u6539\u540e\u68c0\u67e5\u4e8b\u60c5\u662f\u5426\u8fdb\u5c55\u987a\u5229\u3002 \u8fd9\u53ef\u4ee5\u907f\u514d\u5728\u4f7f\u7528\u4e0b\u62c9\u83dc\u5355\u3001\u5de5\u5177\u63d0\u793a\u7b49\u65f6\u51fa\u73b0\u5f88\u591a\u4ee4\u4eba\u5934\u75bc\u7684\u95ee\u9898\uff0c\u56e0\u4e3a\u73b0\u5728\u6211\u4eec\u4e0d\u5fc5\u7f16\u5199\u81ea\u5b9a\u4e49\u903b\u8f91\u6765\u68c0\u67e5\u6ea2\u51fa\u60c5\u51b5\u3002 \u6f14\u793a \u8fd9\u662f\u6211\u4f7f\u7528\u4e0a\u8ff0 css \u5c5e\u6027\u7f16\u5199\u7684\u4ee3\u7801\u7684\u5feb\u901f\u6f14\u793a\uff1a \u6211\u90a3\u4e11\u964b\u7684\u5bfc\u822a\u680f\u4e2d\u7684\u5b50\u83dc\u5355\u4f1a\u6839\u636e\u89c6\u53e3\u7684\u5bbd\u5ea6\u6539\u53d8\u5176\u4f4d\u7f6e\u3002 \u4ee3\u7801\u662f\u7528react\u7f16\u5199\u7684\u3002\u65e9\u4e9b\u65f6\u5019\u6211\u5fc5\u987b\u4f7f\u7528\u6548\u679c\u6765\u505a\u5230\u8fd9\u4e00\u70b9\u3002\u5728\u6211\u7684\u6548\u679c\u4e2d\uff0c\u6211\u4f1a\u68c0\u67e5\u5b50\u83dc\u5355\u5143\u7d20\u662f\u5426\u8de8\u8d8a\u89c6\u53e3\u7684\u8fb9\u754c\u3002\u5982\u679c\u662f\uff0c\u6211\u4f1a\u89e6\u53d1\u53e6\u4e00\u4e2a\u91cd\u65b0\u6e32\u67d3\u6765\u66f4\u65b0\u5b50\u83dc\u5355\u7684\u6837\u5f0f\u3002\u7531\u4e8e useeffect \u5728\u7ed8\u5236\u4e4b\u540e\u8fd0\u884c\uff0c\u5e76\u4e14\u6211\u4eec\u4e0d\u5e0c\u671b\u7528\u6237\u5728\u9519\u8bef\u7684\u4f4d\u7f6e\u770b\u5230\u5b50\u83dc\u5355\uff0c\u56e0\u6b64\u6211\u5fc5\u987b\u4e3a\u6b64\u4f7f\u7528 uselayouteffect\u3002 \u73b0\u5728\u6211\u6240\u8981\u505a\u7684\u5c31\u662f\u7f16\u5199\u8fd9\u6837\u7684 css\uff1a .button-anchor { anchor-name: &#8211;anchorButton; } @position-try &#8211;bottom { position-area: bottom; [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[16],"tags":[],"class_list":["post-52059","post","type-post","status-publish","format-standard","hentry","category-16"],"_links":{"self":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/52059","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/comments?post=52059"}],"version-history":[{"count":0,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/52059\/revisions"}],"wp:attachment":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/media?parent=52059"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/categories?post=52059"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/tags?post=52059"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}