{"id":1182,"date":"2024-11-10T09:09:44","date_gmt":"2024-11-10T01:09:44","guid":{"rendered":"https:\/\/fwq.ai\/blog\/1182\/"},"modified":"2024-11-10T09:09:44","modified_gmt":"2024-11-10T01:09:44","slug":"%e5%a6%82%e4%bd%95%e5%9c%a8vue%e4%b8%ad%e5%b0%86%e4%b8%a4%e5%bc%a0%e5%9b%be%e7%89%87%e5%90%88%e5%b9%b6%e5%b9%b6%e5%ae%9e%e7%8e%b0%e5%93%8d%e5%ba%94%e5%bc%8f%e9%80%82%e9%85%8d%ef%bc%9f","status":"publish","type":"post","link":"https:\/\/fwq.ai\/blog\/1182\/","title":{"rendered":"\u5982\u4f55\u5728Vue\u4e2d\u5c06\u4e24\u5f20\u56fe\u7247\u5408\u5e76\u5e76\u5b9e\u73b0\u54cd\u5e94\u5f0f\u9002\u914d\uff1f"},"content":{"rendered":"<p><img decoding=\"async\" src=\"https:\/\/img.php.cn\/upload\/article\/001\/246\/273\/173028618489360.jpg\" class=\"aligncenter\" title=\"\u5982\u4f55\u5728Vue\u4e2d\u5c06\u4e24\u5f20\u56fe\u7247\u5408\u5e76\u5e76\u5b9e\u73b0\u54cd\u5e94\u5f0f\u9002\u914d\uff1f\u63d2\u56fe\" alt=\"\u5982\u4f55\u5728Vue\u4e2d\u5c06\u4e24\u5f20\u56fe\u7247\u5408\u5e76\u5e76\u5b9e\u73b0\u54cd\u5e94\u5f0f\u9002\u914d\uff1f\u63d2\u56fe\" \/><\/p>\n<p><strong>\u4e2d\u5c06\u4e24\u5f20\u56fe\u7247\u5408\u5e76\u5e76\u54cd\u5e94\u6240\u6709\u9875\u9762\u5927\u5c0f\u7684\u9002\u914d<\/strong><\/p>\n<p>\u5728vue\u4e2d\uff0c\u5c06\u4e24\u5f20\u56fe\u7247\u5408\u5e76\u5e76\u5b9e\u73b0\u54cd\u5e94\u5f0f\u9002\u914d\u53ef\u4ee5\u91c7\u7528\u52a8\u6001\u5355\u4f4d\u548c@media\u5a92\u4f53\u67e5\u8be2\u7ed3\u5408\u7684\u65b9\u5f0f\u3002<\/p>\n<p><strong>\u52a8\u6001\u5355\u4f4d<\/strong><br \/>\u53ef\u4ee5\u4f7f\u7528\u89c6\u53e3\u5bbd\u5ea6\u5355\u4f4dvw\u6216\u54cd\u5e94\u6027\u6839\u5b57\u4f53\u5927\u5c0f\u5355\u4f4drem\u3002vw\u662f\u4e00\u4e2a\u6d4f\u89c8\u5668\u5185\u7f6e\u7684\u52a8\u6001\u5355\u4f4d\uff0c100vw\u7b49\u4e8e\u9875\u9762\u53ef\u89c6\u533a\u57df\u5bbd\u5ea6\uff0c\u53ef\u4ee5\u6839\u636e\u4e0d\u540c\u5c4f\u5e55\u5bbd\u5ea6\u5b9e\u73b0\u52a8\u6001\u8c03\u6574\u3002rem\u4e0e\u6839\u5143\u7d20html\u7684\u5b57\u4f53\u5927\u5c0f\u76f8\u5173\uff0c\u53ef\u4ee5\u901a\u8fc7\u52a8\u6001\u8ba1\u7b97\u6839\u5b57\u4f53\u5927\u5c0f\u6765\u5b9e\u73b0\u9002\u5e94\u3002<\/p>\n<p><strong>@media\u5a92\u4f53\u67e5\u8be2<\/strong><br \/>@media\u5a92\u4f53\u67e5\u8be2\u53ef\u4ee5\u6839\u636e\u5c4f\u5e55\u5bbd\u5ea6\u8bbe\u7f6e\u4e0d\u540c\u7684\u6837\u5f0f\u3002\u4f8b\u5982\uff0c\u5bf9\u4e8e\u6700\u5927\u5bbd\u5ea6\u4e3a365px\u7684\u8bbe\u5907\uff0c\u53ef\u4ee5\u8bbe\u7f6e\u4e0d\u540c\u7684\u6837\u5f0f\u6765\u5b9e\u73b0\u56fe\u7247\u5408\u5e76\u3002<\/p>\n<p><span>\u7acb\u5373\u5b66\u4e60<\/span>\u201c\u201d\uff1b<\/p>\n<p><strong>\u793a\u4f8b\u4ee3\u7801<\/strong><\/p>\n<pre>&lt;template&gt;\n  &lt;div class=\"container\"&gt;\n    @@##@@\n    @@##@@\n  &lt;\/div&gt;\n&lt;\/template&gt;\n\n&lt;script&gt;\nimport { onMounted } from 'vue';\n\nexport default {\n  data() {\n    return {\n      imgUrl1: '\u56fe\u72471\u5730\u5740',\n      imgUrl2: '\u56fe\u72472\u5730\u5740',\n    };\n  },\n  mounted() {\n    \/\/ \u52a8\u6001\u8ba1\u7b97\u6839\u5b57\u4f53\u5927\u5c0f\uff0c\u57fa\u4e8e\u89c6\u53e3\u5bbd\u5ea6\n    const calcRem = () =&gt; {\n      const docWidth = document.body.clientWidth;\n      const rootFontSize = calcVw(docWidth \/ 375 * 100, 16);\n      document.documentElement.style.fontSize = `${rootFontSize}px`;\n    };\n\n    \/\/ \u5a92\u4f53\u67e5\u8be2\uff0c\u9488\u5bf9\u6700\u5927\u5bbd\u5ea6\u4e3a365px\u7684\u8bbe\u5907\n    const query = window.matchMedia('(max-width: 365px)');\n    const handleMediaQuery = e =&gt; {\n      if (e.matches) {\n        \/\/ \u8fd9\u91cc\u5199\u9488\u5bf9\u6700\u5927\u5bbd\u5ea6\u4e3a365px\u7684\u8bbe\u5907\u7684\u6837\u5f0f\u8c03\u6574\u903b\u8f91\n      } else {\n        \/\/ \u8fd9\u91cc\u5199\u9488\u5bf9\u975e\u6700\u5927\u5bbd\u5ea6\u4e3a365px\u7684\u8bbe\u5907\u7684\u6837\u5f0f\u8c03\u6574\u903b\u8f91\n      }\n    };\n    query.addListener(handleMediaQuery);\n\n    onMounted(() =&gt; {\n      calcRem();\n    });\n  },\n};\n&lt;\/script&gt;\n\n&lt;style&gt;\n.container {\n  display: flex;\n}\n\nimg {\n  width: 50%;   \/\/ \u8fd9\u91cc\u4e5f\u53ef\u4ee5\u4f7f\u7528\u52a8\u6001\u5355\u4f4d\uff0c\u5982vw\n}\n\n@media (max-width: 365px) {\n  \/* \u9488\u5bf9\u6700\u5927\u5bbd\u5ea6\u4e3a365px\u7684\u8bbe\u5907\u7684\u6837\u5f0f\u8c03\u6574 *\/\n}\n&lt;\/style&gt;<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p><strong>\u539f\u7406<\/strong><br \/>\u8be5\u793a\u4f8b\u901a\u8fc7onmounted\u751f\u547d\u5468\u671f\u94a9\u5b50\u5728\u9875\u9762\u52a0\u8f7d\u5b8c\u6210\u65f6\u89e6\u53d1calcrem\u51fd\u6570\uff0c\u6839\u636e\u89c6\u53e3\u5bbd\u5ea6\u8ba1\u7b97\u6839\u5b57\u4f53\u5927\u5c0f\uff0c\u4ece\u800c\u5b9e\u73b0\u54cd\u5e94\u5f0f\u3002\u5e76\u4f7f\u7528@media\u5a92\u4f53\u67e5\u8be2\u9488\u5bf9\u4e0d\u540c\u5c4f\u5e55\u5bbd\u5ea6\u8bbe\u7f6e\u4e0d\u540c\u7684\u6837\u5f0f\uff0c\u5b9e\u73b0\u56fe\u7247\u5408\u5e76\u7684\u9002\u914d\u3002<\/p>\n<p><strong>\u6548\u679c<\/strong><br \/>\u4f7f\u7528\u52a8\u6001\u5355\u4f4d\u548c\u5a92\u4f53\u67e5\u8be2\uff0c\u56fe\u7247\u5c06\u6839\u636e\u9875\u9762\u5927\u5c0f\u52a8\u6001\u8c03\u6574\u5c3a\u5bf8\u548c\u4f4d\u7f6e\uff0c\u5728\u6240\u6709\u5927\u5c0f\u7684\u9875\u9762\u4e2d\u90fd\u80fd\u6b63\u786e\u663e\u793a\u548c\u5408\u5e76\u3002<\/p>\n<p><img><br \/>\n<img> <\/p>\n<p>\u4ee5\u4e0a\u5c31\u662f\u5982\u4f55\u5728Vue\u4e2d\u5c06\u4e24\u5f20\u56fe\u7247\u5408\u5e76\u5e76\u5b9e\u73b0\u54cd\u5e94\u5f0f\u9002\u914d\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>\u4e2d\u5c06\u4e24\u5f20\u56fe\u7247\u5408\u5e76\u5e76\u54cd\u5e94\u6240\u6709\u9875\u9762\u5927\u5c0f\u7684\u9002\u914d \u5728vue\u4e2d\uff0c\u5c06\u4e24\u5f20\u56fe\u7247\u5408\u5e76\u5e76\u5b9e\u73b0\u54cd\u5e94\u5f0f\u9002\u914d\u53ef\u4ee5\u91c7\u7528\u52a8\u6001\u5355\u4f4d\u548c@media\u5a92\u4f53\u67e5\u8be2\u7ed3\u5408\u7684\u65b9\u5f0f\u3002 \u52a8\u6001\u5355\u4f4d\u53ef\u4ee5\u4f7f\u7528\u89c6\u53e3\u5bbd\u5ea6\u5355\u4f4dvw\u6216\u54cd\u5e94\u6027\u6839\u5b57\u4f53\u5927\u5c0f\u5355\u4f4drem\u3002vw\u662f\u4e00\u4e2a\u6d4f\u89c8\u5668\u5185\u7f6e\u7684\u52a8\u6001\u5355\u4f4d\uff0c100vw\u7b49\u4e8e\u9875\u9762\u53ef\u89c6\u533a\u57df\u5bbd\u5ea6\uff0c\u53ef\u4ee5\u6839\u636e\u4e0d\u540c\u5c4f\u5e55\u5bbd\u5ea6\u5b9e\u73b0\u52a8\u6001\u8c03\u6574\u3002rem\u4e0e\u6839\u5143\u7d20html\u7684\u5b57\u4f53\u5927\u5c0f\u76f8\u5173\uff0c\u53ef\u4ee5\u901a\u8fc7\u52a8\u6001\u8ba1\u7b97\u6839\u5b57\u4f53\u5927\u5c0f\u6765\u5b9e\u73b0\u9002\u5e94\u3002 @media\u5a92\u4f53\u67e5\u8be2@media\u5a92\u4f53\u67e5\u8be2\u53ef\u4ee5\u6839\u636e\u5c4f\u5e55\u5bbd\u5ea6\u8bbe\u7f6e\u4e0d\u540c\u7684\u6837\u5f0f\u3002\u4f8b\u5982\uff0c\u5bf9\u4e8e\u6700\u5927\u5bbd\u5ea6\u4e3a365px\u7684\u8bbe\u5907\uff0c\u53ef\u4ee5\u8bbe\u7f6e\u4e0d\u540c\u7684\u6837\u5f0f\u6765\u5b9e\u73b0\u56fe\u7247\u5408\u5e76\u3002 \u7acb\u5373\u5b66\u4e60\u201c\u201d\uff1b \u793a\u4f8b\u4ee3\u7801 &lt;template&gt; &lt;div class=&#8221;container&#8221;&gt; @@##@@ @@##@@ &lt;\/div&gt; &lt;\/template&gt; &lt;script&gt; import { onMounted } from &#8216;vue&#8217;; export default { data() { return { imgUrl1: &#8216;\u56fe\u72471\u5730\u5740&#8217;, imgUrl2: &#8216;\u56fe\u72472\u5730\u5740&#8217;, }; }, mounted() { \/\/ \u52a8\u6001\u8ba1\u7b97\u6839\u5b57\u4f53\u5927\u5c0f\uff0c\u57fa\u4e8e\u89c6\u53e3\u5bbd\u5ea6 const calcRem = () =&gt; { const docWidth = document.body.clientWidth; const rootFontSize = calcVw(docWidth \/ 375 * 100, [&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-1182","post","type-post","status-publish","format-standard","hentry","category-16"],"_links":{"self":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/1182","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=1182"}],"version-history":[{"count":0,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/1182\/revisions"}],"wp:attachment":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/media?parent=1182"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/categories?post=1182"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/tags?post=1182"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}