{"id":26453,"date":"2024-11-24T12:05:37","date_gmt":"2024-11-24T04:05:37","guid":{"rendered":"https:\/\/fwq.ai\/blog\/26453\/"},"modified":"2024-11-24T12:05:37","modified_gmt":"2024-11-24T04:05:37","slug":"%e5%a6%82%e4%bd%95%e5%9c%a8-vue-3-%e4%b8%ad%e5%ae%9e%e7%8e%b0%e5%b1%80%e9%83%a8%e9%a1%b5%e9%9d%a2%e8%87%aa%e9%80%82%e5%ba%94-px-to-rem%ef%bc%9f-2","status":"publish","type":"post","link":"https:\/\/fwq.ai\/blog\/26453\/","title":{"rendered":"\u5982\u4f55\u5728 Vue 3 \u4e2d\u5b9e\u73b0\u5c40\u90e8\u9875\u9762\u81ea\u9002\u5e94 px to rem\uff1f"},"content":{"rendered":"<p><img decoding=\"async\" src=\"https:\/\/img.php.cn\/upload\/article\/001\/246\/273\/173095296864035.jpg\" class=\"aligncenter\" title=\"\u5982\u4f55\u5728 Vue 3 \u4e2d\u5b9e\u73b0\u5c40\u90e8\u9875\u9762\u81ea\u9002\u5e94 px to rem\uff1f\u63d2\u56fe\" alt=\"\u5982\u4f55\u5728 Vue 3 \u4e2d\u5b9e\u73b0\u5c40\u90e8\u9875\u9762\u81ea\u9002\u5e94 px to rem\uff1f\u63d2\u56fe\" \/><\/p>\n<p><strong> 3 \u4e2d\u5c40\u90e8\u81ea\u9002\u5e94 px to rem<\/strong><\/p>\n<p>\u5bf9\u4e8e Vue \u9879\u76ee\u4e2d\u7684\u67d0\u4e2a\u9875\u9762\u8fdb\u884c\u81ea\u9002\u5e94\u8f6c\u6362\uff0c\u9700\u8981\u786e\u4fdd\u8f6c\u6362\u53ea\u5f71\u54cd\u8be5\u9875\u9762\uff0c\u800c\u4e0d\u4f1a\u7834\u574f\u6574\u4e2a\u9879\u76ee\u7684 UI \u6846\u67b6\u3002\u4f20\u7edf\u65b9\u6cd5\uff0c\u4f8b\u5982 post-px-to-rem \u548c @njleonzhang\/postcss-px-to-rem\uff0c\u7531\u4e8e\u5f71\u54cd\u5168\u5c40\uff0c\u56e0\u6b64\u4e0d\u9002\u7528\u4e8e\u6b64\u573a\u666f\u3002<\/p>\n<p>\u4e00\u79cd\u53ef\u884c\u7684\u89e3\u51b3\u65b9\u6848\u662f\u4f7f\u7528 JavaScript \u52a8\u6001\u8ba1\u7b97\u50cf\u7d20\u503c\u5e76\u5c06\u5176\u8f6c\u6362\u4e3a rem\u3002\u6211\u4eec\u4ee5 JQuery \u4e3a\u4f8b\uff1a<\/p>\n<pre>let appWidth = $('#app').width()\nlet size = (appWidth \/ 375) * 10\ndocument.documentElement.style.fontSize = size + 'px';<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p>\u9996\u5148\uff0c\u6211\u4eec\u83b7\u53d6\u9875\u9762\u5bbd\u5ea6\u5e76\u5c06\u5176\u9664\u4ee5\u57fa\u51c6\u5bbd\u5ea6\uff08\u672c\u4f8b\u4e2d\u4e3a 375px\uff09\u6765\u8ba1\u7b97\u6bd4\u4f8b\u3002\u7136\u540e\uff0c\u6211\u4eec\u6839\u636e\u57fa\u51c6\u5b57\u4f53\u5927\u5c0f\uff08\u672c\u4f8b\u4e2d\u4e3a 10px\uff09\u5c06\u6bd4\u4f8b\u8f6c\u6362\u6210 rem \u503c\u3002\u6700\u540e\uff0c\u6211\u4eec\u5c06\u8ba1\u7b97\u51fa\u7684 rem \u503c\u8bbe\u7f6e\u5230 documentElement \u7684 fontSize \u5c5e\u6027\u4e0a\u3002<\/p>\n<p>\u9700\u8981\u6ce8\u610f\u7684\u662f\uff0c\u6b64\u89e3\u51b3\u65b9\u6848\u4ec5\u9002\u7528\u4e8e\u9875\u9762\u7ea7\u522b\u7684\u81ea\u9002\u5e94\u3002\u5982\u679c\u60a8\u9700\u8981\u5728\u7ec4\u4ef6\u7ea7\u522b\u8fdb\u884c\u81ea\u9002\u5e94\uff0c\u5219\u53ef\u4ee5\u4f7f\u7528 CSS Variables \u6216\u7b2c\u4e09\u65b9\u5e93\u3002<\/p>\n<p><span>\u7acb\u5373\u5b66\u4e60<\/span>\u201c\u201d\uff1b<\/p>\n<p>\u4ee5\u4e0a\u5c31\u662f\u5982\u4f55\u5728 Vue 3 \u4e2d\u5b9e\u73b0\u5c40\u90e8\u9875\u9762\u81ea\u9002\u5e94 px to rem\uff1f\u7684\u8be6\u7ec6\u5185\u5bb9\uff0c\u66f4\u591a\u8bf7\u5173\u6ce8\u7c73\u4e91\u5176\u5b83\u76f8\u5173\u6587\u7ae0\uff01<\/p>\n","protected":false},"excerpt":{"rendered":"<p>3 \u4e2d\u5c40\u90e8\u81ea\u9002\u5e94 px to rem \u5bf9\u4e8e Vue \u9879\u76ee\u4e2d\u7684\u67d0\u4e2a\u9875\u9762\u8fdb\u884c\u81ea\u9002\u5e94\u8f6c\u6362\uff0c\u9700\u8981\u786e\u4fdd\u8f6c\u6362\u53ea\u5f71\u54cd\u8be5\u9875\u9762\uff0c\u800c\u4e0d\u4f1a\u7834\u574f\u6574\u4e2a\u9879\u76ee\u7684 UI \u6846\u67b6\u3002\u4f20\u7edf\u65b9\u6cd5\uff0c\u4f8b\u5982 post-px-to-rem \u548c @njleonzhang\/postcss-px-to-rem\uff0c\u7531\u4e8e\u5f71\u54cd\u5168\u5c40\uff0c\u56e0\u6b64\u4e0d\u9002\u7528\u4e8e\u6b64\u573a\u666f\u3002 \u4e00\u79cd\u53ef\u884c\u7684\u89e3\u51b3\u65b9\u6848\u662f\u4f7f\u7528 JavaScript \u52a8\u6001\u8ba1\u7b97\u50cf\u7d20\u503c\u5e76\u5c06\u5176\u8f6c\u6362\u4e3a rem\u3002\u6211\u4eec\u4ee5 JQuery \u4e3a\u4f8b\uff1a let appWidth = $(&#8216;#app&#8217;).width() let size = (appWidth \/ 375) * 10 document.documentElement.style.fontSize = size + &#8216;px&#8217;; \u767b\u5f55\u540e\u590d\u5236 \u9996\u5148\uff0c\u6211\u4eec\u83b7\u53d6\u9875\u9762\u5bbd\u5ea6\u5e76\u5c06\u5176\u9664\u4ee5\u57fa\u51c6\u5bbd\u5ea6\uff08\u672c\u4f8b\u4e2d\u4e3a 375px\uff09\u6765\u8ba1\u7b97\u6bd4\u4f8b\u3002\u7136\u540e\uff0c\u6211\u4eec\u6839\u636e\u57fa\u51c6\u5b57\u4f53\u5927\u5c0f\uff08\u672c\u4f8b\u4e2d\u4e3a 10px\uff09\u5c06\u6bd4\u4f8b\u8f6c\u6362\u6210 rem \u503c\u3002\u6700\u540e\uff0c\u6211\u4eec\u5c06\u8ba1\u7b97\u51fa\u7684 rem \u503c\u8bbe\u7f6e\u5230 documentElement \u7684 fontSize \u5c5e\u6027\u4e0a\u3002 \u9700\u8981\u6ce8\u610f\u7684\u662f\uff0c\u6b64\u89e3\u51b3\u65b9\u6848\u4ec5\u9002\u7528\u4e8e\u9875\u9762\u7ea7\u522b\u7684\u81ea\u9002\u5e94\u3002\u5982\u679c\u60a8\u9700\u8981\u5728\u7ec4\u4ef6\u7ea7\u522b\u8fdb\u884c\u81ea\u9002\u5e94\uff0c\u5219\u53ef\u4ee5\u4f7f\u7528 CSS Variables \u6216\u7b2c\u4e09\u65b9\u5e93\u3002 \u7acb\u5373\u5b66\u4e60\u201c\u201d\uff1b \u4ee5\u4e0a\u5c31\u662f\u5982\u4f55\u5728 Vue [&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-26453","post","type-post","status-publish","format-standard","hentry","category-16"],"_links":{"self":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/26453","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=26453"}],"version-history":[{"count":0,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/26453\/revisions"}],"wp:attachment":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/media?parent=26453"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/categories?post=26453"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/tags?post=26453"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}