{"id":1559,"date":"2024-11-10T09:09:07","date_gmt":"2024-11-10T01:09:07","guid":{"rendered":"https:\/\/fwq.ai\/blog\/1559\/"},"modified":"2024-11-10T09:09:07","modified_gmt":"2024-11-10T01:09:07","slug":"vue-3-%e4%b8%ad%e5%a6%82%e4%bd%95%e8%8e%b7%e5%8f%96%e5%85%83%e7%b4%a0%e7%9a%84-margin-top-%e5%80%bc%ef%bc%9f","status":"publish","type":"post","link":"https:\/\/fwq.ai\/blog\/1559\/","title":{"rendered":"Vue 3 \u4e2d\u5982\u4f55\u83b7\u53d6\u5143\u7d20\u7684 margin-top \u503c\uff1f"},"content":{"rendered":"<p><img decoding=\"async\" src=\"https:\/\/img.php.cn\/upload\/article\/001\/246\/273\/173046657775065.jpg\" class=\"aligncenter\" title=\"Vue 3 \u4e2d\u5982\u4f55\u83b7\u53d6\u5143\u7d20\u7684 margin-top \u503c\uff1f\u63d2\u56fe\" alt=\"Vue 3 \u4e2d\u5982\u4f55\u83b7\u53d6\u5143\u7d20\u7684 margin-top \u503c\uff1f\u63d2\u56fe\" \/><\/p>\n<p><strong>\u5982\u4f55\u5728  3 \u4e2d\u83b7\u53d6\u5143\u7d20\u7684 margin-top \u503c<\/strong><\/p>\n<p><strong>\u95ee\u9898\u63cf\u8ff0\uff1a<\/strong><\/p>\n<p>\u5728 vue 3 \u4e2d\uff0c\u9700\u8981\u52a8\u6001\u83b7\u53d6 dom \u5143\u7d20\u7684 margin-top \u503c\u4ee5\u5b9e\u73b0\u52a8\u753b\u6548\u679c\u3002\u5982\u4f55\u5b9e\u73b0\u8fd9\u4e00\u76ee\u6807\uff1f<\/p>\n<p><strong>\u89e3\u51b3\u65b9\u6848\uff1a<\/strong><\/p>\n<p><span>\u7acb\u5373\u5b66\u4e60<\/span>\u201c\u201d\uff1b<\/p>\n<p><strong>\u786e\u4fdd\u5143\u7d20\u5df2\u6e32\u67d3\u5b8c\u6bd5<\/strong><\/p>\n<p>\u5728\u83b7\u53d6\u5143\u7d20\u7684 margin-top \u503c\u4e4b\u524d\uff0c\u9700\u8981\u786e\u4fdd\u76ee\u6807\u5143\u7d20\u5df2\u7ecf\u6e32\u67d3\u5b8c\u6bd5\u3002\u8fd9\u53ef\u4ee5\u901a\u8fc7\u4f7f\u7528 mounted \u751f\u547d\u5468\u671f\u94a9\u5b50\u6216 nexttick \u51fd\u6570\u6765\u5b9e\u73b0\u3002<\/p>\n<p><strong>\u83b7\u53d6 margin-top \u503c<\/strong><\/p>\n<p>\u83b7\u53d6 margin-top \u503c\u7684\u65b9\u6cd5\u5982\u4e0b\uff1a<\/p>\n<ol>\n<li><strong>composition api\uff1a<\/strong><\/li>\n<\/ol>\n<pre>&lt;div ref=\"targetdom\"&gt;&lt;\/idv&gt;<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<pre>\/\/ \u8fd9\u91cc\u4e0d\u4f20\u503c\uff0c\u53d8\u91cf\u540d\u8981\u7b49\u4e8e ref\nconst targetdom = ref&lt;htmlelement&gt;()\n\nfunction getmargintop(): string {\n  const styles = getcomputedstyle(targetdom.value);\n  return styles.getpropertyvalue('margintop');\n}<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<ol>\n<li><strong>options api\uff1a<\/strong><\/li>\n<\/ol>\n<pre>&lt;div ref=\"targetdom\"&gt;&lt;\/idv&gt;<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<pre>export default {\n  mounted() {\n    const targetDom = this.$refs.targetDom as HTMLElement;\n    const marginTop = window.getComputedStyle(targetDom).marginTop;\n  }\n};<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p>\u4ee5\u4e0a\u5c31\u662fVue 3 \u4e2d\u5982\u4f55\u83b7\u53d6\u5143\u7d20\u7684 margin-top \u503c\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>\u5982\u4f55\u5728 3 \u4e2d\u83b7\u53d6\u5143\u7d20\u7684 margin-top \u503c \u95ee\u9898\u63cf\u8ff0\uff1a \u5728 vue 3 \u4e2d\uff0c\u9700\u8981\u52a8\u6001\u83b7\u53d6 dom \u5143\u7d20\u7684 margin-top \u503c\u4ee5\u5b9e\u73b0\u52a8\u753b\u6548\u679c\u3002\u5982\u4f55\u5b9e\u73b0\u8fd9\u4e00\u76ee\u6807\uff1f \u89e3\u51b3\u65b9\u6848\uff1a \u7acb\u5373\u5b66\u4e60\u201c\u201d\uff1b \u786e\u4fdd\u5143\u7d20\u5df2\u6e32\u67d3\u5b8c\u6bd5 \u5728\u83b7\u53d6\u5143\u7d20\u7684 margin-top \u503c\u4e4b\u524d\uff0c\u9700\u8981\u786e\u4fdd\u76ee\u6807\u5143\u7d20\u5df2\u7ecf\u6e32\u67d3\u5b8c\u6bd5\u3002\u8fd9\u53ef\u4ee5\u901a\u8fc7\u4f7f\u7528 mounted \u751f\u547d\u5468\u671f\u94a9\u5b50\u6216 nexttick \u51fd\u6570\u6765\u5b9e\u73b0\u3002 \u83b7\u53d6 margin-top \u503c \u83b7\u53d6 margin-top \u503c\u7684\u65b9\u6cd5\u5982\u4e0b\uff1a composition api\uff1a &lt;div ref=&#8221;targetdom&#8221;&gt;&lt;\/idv&gt; \u767b\u5f55\u540e\u590d\u5236 \u767b\u5f55\u540e\u590d\u5236 \/\/ \u8fd9\u91cc\u4e0d\u4f20\u503c\uff0c\u53d8\u91cf\u540d\u8981\u7b49\u4e8e ref const targetdom = ref&lt;htmlelement&gt;() function getmargintop(): string { const styles = getcomputedstyle(targetdom.value); return styles.getpropertyvalue(&#8216;margintop&#8217;); } \u767b\u5f55\u540e\u590d\u5236 [&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-1559","post","type-post","status-publish","format-standard","hentry","category-16"],"_links":{"self":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/1559","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=1559"}],"version-history":[{"count":0,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/1559\/revisions"}],"wp:attachment":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/media?parent=1559"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/categories?post=1559"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/tags?post=1559"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}