{"id":1502,"date":"2024-11-10T16:37:48","date_gmt":"2024-11-10T08:37:48","guid":{"rendered":"https:\/\/fwq.ai\/blog\/1502\/"},"modified":"2024-11-10T16:37:48","modified_gmt":"2024-11-10T08:37:48","slug":"%e5%a6%82%e4%bd%95%e5%9c%a8-vue3-%e4%b8%ad%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\/1502\/","title":{"rendered":"\u5982\u4f55\u5728 Vue3 \u4e2d\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\/173045001037420.jpg\" class=\"aligncenter\" title=\"\u5982\u4f55\u5728 Vue3 \u4e2d\u83b7\u53d6\u5143\u7d20\u7684 margin-top \u503c\uff1f\u63d2\u56fe\" alt=\"\u5982\u4f55\u5728 Vue3 \u4e2d\u83b7\u53d6\u5143\u7d20\u7684 margin-top \u503c\uff1f\u63d2\u56fe\" \/><\/p>\n<p><strong>\u83b7\u53d6 3 \u4e2d\u5143\u7d20\u7684 margin-top \u503c<\/strong><\/p>\n<p>\u5728 vue3 \u4e2d\u52a8\u6001\u83b7\u53d6 dom \u5143\u7d20\u7684 margin-top \u503c\u5bf9\u4e8e\u521b\u5efa\u52a8\u753b\u6548\u679c\u5f88\u6709\u7528\u3002\u8981\u505a\u5230\u8fd9\u4e00\u70b9\uff1a<\/p>\n<p>\u9996\u5148\uff0c\u786e\u4fdd\u5728\u83b7\u53d6\u503c\u65f6\uff0c\u76ee\u6807\u5143\u7d20\u5df2\u5b8c\u5168\u6e32\u67d3\u3002<\/p>\n<p>\u63a5\u4e0b\u6765\uff0c\u6839\u636e\u4f60\u4f7f\u7528\u7684 api\uff0c\u4f7f\u7528\u4ee5\u4e0b\u65b9\u6cd5\uff1a<\/p>\n<p><span>\u7acb\u5373\u5b66\u4e60<\/span>\u201c\u201d\uff1b<\/p>\n<p><strong>composition api\uff1a<\/strong><\/p>\n<ol>\n<li>\n<p>\u4e3a\u76ee\u6807\u5143\u7d20\u6dfb\u52a0\u4e00\u4e2a ref\uff1a<\/p>\n<pre>&lt;div ref=\"targetdom\"&gt;&lt;\/div&gt;<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/li>\n<li>\n<p>\u5728 setup \u51fd\u6570\u4e2d\uff0c\u521b\u5efa\u4e00\u4e2a ref \u5bf9\u8c61\uff1a<\/p>\n<pre>const targetdom = ref&lt;htmlelement&gt;();<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/li>\n<li>\n<p>\u5b9a\u4e49\u4e00\u4e2a\u51fd\u6570\u6765\u83b7\u53d6 margin-top \u503c\uff1a<\/p>\n<pre>function getMarginTop(): string {\n  const styles = getComputedStyle(targetDom.value);\n  return styles.getPropertyValue('marginTop');\n}<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/li>\n<\/ol>\n<p>\u4f60\u73b0\u5728\u53ef\u4ee5\u4f7f\u7528 getmargintop() \u51fd\u6570\u6765\u83b7\u53d6 margin-top \u503c\u3002<\/p>\n<p>\u4ee5\u4e0a\u5c31\u662f\u5982\u4f55\u5728 Vue3 \u4e2d\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>\u83b7\u53d6 3 \u4e2d\u5143\u7d20\u7684 margin-top \u503c \u5728 vue3 \u4e2d\u52a8\u6001\u83b7\u53d6 dom \u5143\u7d20\u7684 margin-top \u503c\u5bf9\u4e8e\u521b\u5efa\u52a8\u753b\u6548\u679c\u5f88\u6709\u7528\u3002\u8981\u505a\u5230\u8fd9\u4e00\u70b9\uff1a \u9996\u5148\uff0c\u786e\u4fdd\u5728\u83b7\u53d6\u503c\u65f6\uff0c\u76ee\u6807\u5143\u7d20\u5df2\u5b8c\u5168\u6e32\u67d3\u3002 \u63a5\u4e0b\u6765\uff0c\u6839\u636e\u4f60\u4f7f\u7528\u7684 api\uff0c\u4f7f\u7528\u4ee5\u4e0b\u65b9\u6cd5\uff1a \u7acb\u5373\u5b66\u4e60\u201c\u201d\uff1b composition api\uff1a \u4e3a\u76ee\u6807\u5143\u7d20\u6dfb\u52a0\u4e00\u4e2a ref\uff1a &lt;div ref=&#8221;targetdom&#8221;&gt;&lt;\/div&gt; \u767b\u5f55\u540e\u590d\u5236 \u5728 setup \u51fd\u6570\u4e2d\uff0c\u521b\u5efa\u4e00\u4e2a ref \u5bf9\u8c61\uff1a const targetdom = ref&lt;htmlelement&gt;(); \u767b\u5f55\u540e\u590d\u5236 \u5b9a\u4e49\u4e00\u4e2a\u51fd\u6570\u6765\u83b7\u53d6 margin-top \u503c\uff1a function getMarginTop(): string { const styles = getComputedStyle(targetDom.value); return styles.getPropertyValue(&#8216;marginTop&#8217;); } \u767b\u5f55\u540e\u590d\u5236 \u4f60\u73b0\u5728\u53ef\u4ee5\u4f7f\u7528 getmargintop() \u51fd\u6570\u6765\u83b7\u53d6 margin-top \u503c\u3002 \u4ee5\u4e0a\u5c31\u662f\u5982\u4f55\u5728 Vue3 [&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-1502","post","type-post","status-publish","format-standard","hentry","category-16"],"_links":{"self":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/1502","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=1502"}],"version-history":[{"count":0,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/1502\/revisions"}],"wp:attachment":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/media?parent=1502"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/categories?post=1502"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/tags?post=1502"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}