{"id":4323,"date":"2014-08-27T18:48:43","date_gmt":"2014-08-27T10:48:43","guid":{"rendered":"http:\/\/www.zhangxinxu.com\/wordpress\/?p=4323"},"modified":"2018-06-15T23:51:02","modified_gmt":"2018-06-15T15:51:02","slug":"svg-viewport-viewbox-preserveaspectratio","status":"publish","type":"post","link":"https:\/\/www.zhangxinxu.com\/wordpress\/2014\/08\/svg-viewport-viewbox-preserveaspectratio\/","title":{"rendered":"\u7406\u89e3SVG viewport,viewBox,preserveAspectRatio\u7f29\u653e"},"content":{"rendered":"<p>by <a href=\"http:\/\/www.zhangxinxu.com\/\">zhangxinxu<\/a> from <a href=\"http:\/\/www.zhangxinxu.com\/\">http:\/\/www.zhangxinxu.com<\/a><br \/>\n\u672c\u6587\u5730\u5740\uff1a<a href=\"http:\/\/www.zhangxinxu.com\/wordpress\/?p=4323\">http:\/\/www.zhangxinxu.com\/wordpress\/?p=4323<\/a><\/p>\n<p>\u4e07\u4e08\u9ad8\u697c\u5e73\u5730\u8d77\uff0c\u57fa\u7840\u5f88\u91cd\u8981\u3002<\/p>\n<h3>viewport<\/h3>\n<p>\u8868\u793aSVG\u53ef\u89c1\u533a\u57df\u7684\u5927\u5c0f\uff0c\u6216\u8005\u53ef\u4ee5\u60f3\u8c61\u6210\u821e\u53f0\u5927\u5c0f\uff0c\u753b\u5e03\u5927\u5c0f\u3002<\/p>\n<div class=\"zxx_code\">\n<pre>&lt;svg width=\"500\" height=\"300\">&lt;\/svg><\/pre>\n<\/div>\n<p>\u4e0a\u9762\u7684SVG\u4ee3\u7801\u5b9a\u4e49\u4e86\u4e00\u4e2a\u89c6\u533a\uff0c\u5bbd500\u5355\u4f4d\uff0c\u9ad8300\u5355\u4f4d\u3002<\/p>\n<p>\u6ce8\u610f\u8fd9\u91cc\u7684\u63aa\u8f9e\u662f\u201c\u5355\u4f4d\u201d\uff0c\u4e0d\u662f\u201c\u50cf\u7d20\u201d\u3002\u867d\u7136\u8bf4\uff0c<code>width<\/code>\/<code>height<\/code>\u5982\u679c\u662f\u7eaf\u6570\u5b57\uff0c\u4f7f\u7528\u7684\u5c31\u662f\u201c\u50cf\u7d20\u201d\u4f5c\u4e3a\u5355\u4f4d\u7684\u3002<\/p>\n<p>\u4e5f\u5c31\u662f\u8bf4\uff0c\u4e0a\u9762SVG\u7684\u89c6\u533a\u5927\u5c0f\u5c31\u662f<code>500px * 300px<\/code>.<\/p>\n<p>\u5f53\u7136\uff0c\u6545\u5f04\u201c\u5355\u4f4d\u201d\u8fd9\u4e2a\u63aa\u8f9e\uff0c\u6f5c\u53f0\u8bcd\u5c31\u662f\u4f60\u53ef\u4ee5\u4f7f\u7528\u5176\u4ed6\u7c7b\u578b\u7684\u5355\u4f4d\uff0c\u6db5\u76d6\u5e38\u89c1CSS\u5355\u4f4d\uff1a<\/p>\n<table width=\"500\" border=\"0\" cellspacing=\"1\" cellpadding=\"0\" class=\"params_table\">\n<tr>\n<th scope=\"col\">\u5355\u4f4d<\/th>\n<th scope=\"col\">\u542b\u4e49<\/th>\n<\/tr>\n<tr>\n<td>em<\/td>\n<td>\u76f8\u5bf9\u4e8e\u7236\u5143\u7d20\u7684\u5b57\u4f53\u5927\u5c0f<\/td>\n<\/tr>\n<tr>\n<td>ex<\/td>\n<td>\u76f8\u5bf9\u4e8e\u5c0f\u5199\u5b57\u6bcd&quot;x&quot;\u7684\u9ad8\u5ea6<\/td>\n<\/tr>\n<tr>\n<td>px<\/td>\n<td>\u76f8\u5bf9\u4e8e\u5c4f\u5e55\u5206\u8fa8\u7387\u800c\u4e0d\u662f\u89c6\u7a97\u5927\u5c0f\uff1a\u901a\u5e38\u4e3a1\u4e2a\u70b9\u62161\/72\u82f1\u5bf8<\/td>\n<\/tr>\n<tr>\n<td>in<\/td>\n<td>inch, \u8868\u82f1\u5bf8<\/td>\n<\/tr>\n<tr>\n<td>cm<\/td>\n<td>centimeter, \u8868\u5398\u7c73<\/td>\n<\/tr>\n<tr>\n<td>mm<\/td>\n<td>millimeter, \u8868\u6beb\u7c73<\/td>\n<\/tr>\n<tr>\n<td>pt<\/td>\n<td>1\/72\u82f1\u5bf8<\/td>\n<\/tr>\n<tr>\n<td>pc<\/td>\n<td>12\u70b9\u6d3b\u5b57\uff0c\u62161\/12\u70b9<\/td>\n<\/tr>\n<tr>\n<td>%<\/td>\n<td>\u76f8\u5bf9\u4e8e\u7236\u5143\u7d20\u3002\u6b63\u5e38\u60c5\u51b5\u4e0b\u662f\u901a\u8fc7\u5c5e\u6027\u5b9a\u4e49\u81ea\u8eab\u6216\u5176\u4ed6\u5143\u7d20<\/td>\n<\/tr>\n<\/table>\n<p>\u9664\u4e86SVG\u672c\u8eab\uff0c\u5176\u4ed6\u4e00\u4e9b\u5143\u7d20\uff0c\u4f8b\u5982<code>&lt;rect&gt;<\/code>\u7684<code>width<\/code>\/<code>height<\/code>\u5c5e\u6027\u4e5f\u53ef\u4ee5\u4f7f\u7528\u4e0a\u9762\u7684\u8fd9\u4e9b\u5355\u4f4d\uff0c\u4e5f\u662f\u9ed8\u8ba4\u5355\u4f4d\u662f\u50cf\u7d20\u3002<\/p>\n<h3>viewBox\u5c5e\u6027<\/h3>\n<p>\u8fd9\u4e2a\u662f\u672c\u6587\u7684\u91cd\u70b9\uff0c\u4e5f\u662f\u96be\u70b9\u3002<\/p>\n<p>\u5148\u770b\u4e00\u4e2a\u6d3b\u8e66\u4e71\u8df3\u7684\u4f8b\u5b50\uff0c\u5982\u4e0bHTML\u4ee3\u7801\uff1a<\/p>\n<div class=\"zxx_code\">\n<pre>&lt;svg width=\"400\" height=\"300\" viewBox=\"0,0,40,30\" style=\"border:1px solid #cd0000;\">\n    &lt;rect x=\"10\" y=\"5\" width=\"20\" height=\"15\" fill=\"#cd0000\"\/>\n&lt;\/svg><\/pre>\n<\/div>\n<p>\u7ed3\u679c\u5982\u4e0b\uff1a<br \/>\n<svg width=\"400\" height=\"300\" viewBox=\"0,0,40,30\" style=\"border:1px solid #cd0000;\"><rect x=\"10\" y=\"5\" width=\"20\" height=\"15\" fill=\"#cd0000\"\/><\/svg><\/p>\n<div id=\"ie68svg\"><\/div>\n<p><script>if (!window.addEventListener) { document.getElementById(\"ie68svg\").innerHTML = '<img decoding=\"async\" src=\"http:\/\/image.zhangxinxu.com\/image\/blog\/201408\/2014-08-26_190011.png\">'; }<\/script><\/p>\n<p>\u6216\u8005\u4eb2\u81ea<a class=\"a_link\" href=\"http:\/\/www.zhangxinxu.com\/study\/201408\/svg-viewbox.html\" target=\"_blank\">\u56f4\u89c2demo<\/a><\/p>\n<p>\u5982\u679c\u4e0d\u770b<code>viewBox<\/code>, \u4f60\u4e00\u5b9a\u4f1a\u89c9\u5f97\u8be7\u5f02\u2014\u2014SVG\u5c3a\u5bf8\u660e\u660e\u6709400*300\u50cf\u7d20\uff0c\u800c\u5c0f\u5c0f\u7684<code>&lt;rect&gt;<\/code>\u5927\u5c0f\u53ea\u6709\u5176<code>1\/20<\/code>\uff0c\u4f46\u662f\u663e\u793a\u51fa\u6765\u7684\u5374\u5360\u636e\u4e86\u534a\u58c1\u6c5f\u5c71\uff01\u4e0d\u79d1\u5b66\u554a\uff01<\/p>\n<p>OK, \u4e4b\u6240\u4ee5\u5c0f\u5c0f\u77e9\u5f62\u5927\u663e\u795e\u5a01\u5c31\u662f\u8fd9\u91cc\u7684<code>viewBox<\/code>\u8d77\u4e86\u63a8\u6ce2\u52a9\u6f9c\u7684\u4f5c\u7528\u3002<\/p>\n<p><code>viewBox<\/code>\u503c\u67094\u4e2a\u6570\u5b57\uff1a<\/p>\n<div class=\"zxx_code\">\n<pre>viewBox=\"x, y, width, height\"  <span style=\"color:green;\">\/\/ x:\u5de6\u4e0a\u89d2\u6a2a\u5750\u6807\uff0cy:\u5de6\u4e0a\u89d2\u7eb5\u5750\u6807\uff0cwidth:\u5bbd\u5ea6\uff0cheight:\u9ad8\u5ea6<\/span><\/pre>\n<\/div>\n<p><code>viewBox<\/code>\u987e\u540d\u601d\u610f\u662f\u201c\u89c6\u533a\u76d2\u5b50\u201d\u7684\u610f\u601d\uff0c\u597d\u6bd4\u5728\u8bf4\uff1a\u201cSVG\u554a\uff0c\u8981\u4e0d\u4f60\u5c31\u8ba9\u6211\u94fa\u6ee1\u4f60\u5427~\u201d<\/p>\n<p>\u66f4\u5f62\u8c61\u7684\u89e3\u91ca\u5c31\u662f\uff1aSVG\u5c31\u50cf\u662f\u6211\u4eec\u7684\u663e\u793a\u5668\u5c4f\u5e55\uff0cviewBox\u5c31\u662f\u622a\u5c4f\u5de5\u5177\u9009\u4e2d\u7684\u90a3\u4e2a\u6846\u6846\uff0c\u6700\u7ec8\u7684\u5448\u73b0\u5c31\u662f\u628a\u6846\u6846\u4e2d\u7684\u622a\u5c4f\u5185\u5bb9\u518d\u6b21\u5728\u663e\u793a\u5668\u4e2d\u5168\u5c4f\u663e\u793a\uff01<\/p>\n<p>\u66f4\u76f4\u89c2\u7684\u89e3\u91ca\uff1a<br \/>\n<strong>1.<\/strong> \u5982\u679c\u6ca1\u6709<code>viewBox<\/code>, \u5e94\u8be5\u662f\u957f\u8fd9\u6837\u7684\uff1a<br \/>\n<img loading=\"lazy\" decoding=\"async\" src=\"http:\/\/image.zhangxinxu.com\/image\/blog\/201408\/2014-08-27_102007.png\" width=\"413\" height=\"316\" class=\"alignnone\" \/><\/p>\n<p><code>&lt;rect&gt;<\/code>\u5927\u5c0f\u53ea\u6709\u6574\u4e2aSVG\u821e\u53f0\u7684<code>1\/20<\/code>.<\/p>\n<p><strong>2.<\/strong> <code>viewBox=\"0,0,40,30\"<\/code>\u76f8\u5f53\u4e8e\u5728SVG\u4e0a\u5708\u4e86\u4e0b\u56fe\u5de6\u4e0a\u89d2\u6240\u793a\u7684\u4e00\u4e2a\u6846\u6846\uff1a<br \/>\n<img loading=\"lazy\" decoding=\"async\" src=\"http:\/\/image.zhangxinxu.com\/image\/blog\/201408\/2014-08-27_102211.png\" width=\"410\" height=\"315\" class=\"alignnone\" \/><\/p>\n<p><strong>3.<\/strong> \u7136\u540e\u628a\u8fd9\u4e2a\u6846\u6846\uff0c\u8fde\u540c\u6846\u6846\u91cc\u7684\u5c0f\u77e9\u5f62\u4e00\u8d77\u653e\u5927\u5230\u6574\u4e2aSVG\u5927\u5c0f\uff08\u5982\u4e0bgif\uff09:<br \/>\n<img loading=\"lazy\" decoding=\"async\" src=\"http:\/\/image.zhangxinxu.com\/image\/blog\/201408\/2014-08-27_105046-viewbox.gif\" width=\"403\" height=\"320\" alt=\"gif\u6f14\u793a\u533a\u57df\u9762\u79ef\u6269\u5927\" class=\"alignnone\" \/><\/p>\n<p>\u5230\u624b\u91cc\u7684\u624d\u662f\u81ea\u5df1\u7684\uff0c\u60a8\u53ef\u4ee5\u72e0\u72e0\u5730\u70b9\u51fb\u8fd9\u91cc\uff1a<a class=\"a_link\" href=\"http:\/\/www.zhangxinxu.com\/study\/201408\/svg-viewbox-explain.html\" target=\"_blank\">SVG viewBox\u5c5e\u6027\u539f\u7406\u5206\u6b65\u6f14\u793ademo<\/a><\/p>\n<p><img loading=\"lazy\" decoding=\"async\" src=\"http:\/\/image.zhangxinxu.com\/image\/blog\/201408\/2014-08-27_112355.png\" width=\"533\" height=\"449\" alt=\"demo\u64cd\u4f5c\u622a\u56fe\u793a\u610f\" class=\"alignnone\" \/><\/p>\n<h3>preserveAspectRatio<\/h3>\n<p>\u4e0a\u9762\u7684\u4f8b\u5b50\uff0cSVG\u7684\u5bbd\u9ad8\u6bd4\u6b63\u597d\u548cviewBox\u7684\u5bbd\u9ad8\u6bd4\u662f\u4e00\u6837\u7684\uff0c\u90fd\u662f<code>4:3<\/code>. \u663e\u7136\uff0c\u5b9e\u9645\u5e94\u7528<code>viewBox<\/code>\u4e0d\u53ef\u80fd\u4e00\u76f4\u8ddf<code>viewport<\/code>\u7a7f\u540c\u4e00\u6761\u5f00\u88c6\u88e4\u3002\u6b64\u65f6\uff0c\u5c31\u9700\u8981<code>preserveAspectRatio<\/code>\u51fa\u9a6c\u4e86\uff0c\u6b64\u5c5e\u6027\u4e5f\u662f\u5e94\u7528\u5728<code>&lt;svg&gt;<\/code>\u5143\u7d20\u4e0a\uff0c\u4e14<strong>\u4f5c\u7528\u7684\u5bf9\u8c61\u90fd\u662fviewBox<\/strong>\u3002<\/p>\n<p>\u5148\u770b\u4e0b\u732a\u662f\u600e\u4e48\u8dd1\u7684\uff1a<\/p>\n<div class=\"zxx_code\">\n<pre>preserveAspectRatio=\"xMidYMid meet\"<\/pre>\n<\/div>\n<p>\u4e0b\u9762\u6211\u4eec\u6765\u5403\u732a\u8089\u3002<\/p>\n<p><code>preserveAspectRatio<\/code>\u5c5e\u6027\u7684\u503c\u4e3a\u7a7a\u683c\u5206\u9694\u7684\u4e24\u4e2a\u503c\u7ec4\u5408\u800c\u6210\u3002\u4f8b\u5982\uff0c\u4e0a\u9762\u7684<code>xMidYMid<\/code>\u548c<code>meet<\/code>.<\/p>\n<p>\u7b2c1\u4e2a\u503c\u8868\u793a\uff0cviewBox\u5982\u4f55\u4e0eSVG viewport\u5bf9\u9f50\uff1b\u7b2c2\u4e2a\u503c\u8868\u793a\uff0c\u5982\u4f55\u7ef4\u6301\u9ad8\u5bbd\u6bd4\uff08\u5982\u679c\u6709\uff09\u3002<\/p>\n<p>\u5176\u4e2d\uff0c\u7b2c1\u4e2a\u503c\u53c8\u662f\u7531\u4e24\u90e8\u5206\u7ec4\u6210\u7684\u3002\u524d\u534a\u90e8\u5206\u8868\u793a<code>x<\/code>\u65b9\u5411\u5bf9\u9f50\uff0c\u540e\u534a\u90e8\u5206\u8868\u793a<code>y<\/code>\u65b9\u5411\u5bf9\u9f50\u3002\u5bb6\u65cf\u6210\u5458\u5982\u4e0b\uff1a<\/p>\n<table width=\"500\" border=\"0\" cellspacing=\"1\" cellpadding=\"0\" class=\"params_table\">\n<tr>\n<th scope=\"col\">\u503c<\/th>\n<th scope=\"col\">\u542b\u4e49<\/th>\n<\/tr>\n<tr>\n<td>xMin<\/td>\n<td>viewport\u548cviewBox\u5de6\u8fb9\u5bf9\u9f50<\/td>\n<\/tr>\n<tr>\n<td>xMid<\/td>\n<td>viewport\u548cviewBox x\u8f74\u4e2d\u5fc3\u5bf9\u9f50<\/td>\n<\/tr>\n<tr>\n<td>xMax<\/td>\n<td>viewport\u548cviewBox\u53f3\u8fb9\u5bf9\u9f50<\/td>\n<\/tr>\n<tr>\n<td>YMin<\/td>\n<td>viewport\u548cviewBox\u4e0a\u8fb9\u7f18\u5bf9\u9f50\u3002\u6ce8\u610fY\u662f\u5927\u5199\u3002<\/td>\n<\/tr>\n<tr>\n<td>YMid<\/td>\n<td>viewport\u548cviewBox y\u8f74\u4e2d\u5fc3\u70b9\u5bf9\u9f50\u3002\u6ce8\u610fY\u662f\u5927\u5199\u3002<\/td>\n<\/tr>\n<tr>\n<td>YMax<\/td>\n<td>viewport\u548cviewBox\u4e0b\u8fb9\u7f18\u5bf9\u9f50\u3002\u6ce8\u610fY\u662f\u5927\u5199\u3002<\/td>\n<\/tr>\n<\/table>\n<p><code>x<\/code>, <code>y<\/code>\u81ea\u7531\u5408\u4f53\u5c31\u53ef\u4ee5\u4e86\uff0c\u5982\uff1a<\/p>\n<div class=\"zxx_code\">\n<pre>xMaxYMax\n\nxMidYMid<\/pre>\n<\/div>\n<p>\u4eb2\u7231\u7684\u5c0f\u4f19\u4f34\uff0c\u770b\u51fa\u5565\u610f\u601d\u6ca1\uff1f<\/p>\n<p>\u5654\u5654\u8e6c\u8e6c\uff0c\u6ca1\u9519\uff0c\u5c31\u662f\u7ec4\u5408\u7684\u610f\u601d\uff1a\u201c\u53f3-\u4e0b\u201d\u548c\u201c\u4e2d-\u4e2d\u201d\u5bf9\u9f50\u3002\u606d\u559c\u4f60\u6b64\u5904\u7684\u77e5\u8bc6\u70b9\u5b66\u4e60\u987a\u5229\u6bd5\u4e1a\uff01<\/p>\n<p><code>preserveAspectRatio<\/code>\u5c5e\u6027\u7b2c2\u90e8\u5206\u7684\u503c\u652f\u6301\u4e0b\u97623\u4e2a\uff1a<\/p>\n<table width=\"500\" border=\"0\" cellspacing=\"1\" cellpadding=\"0\" class=\"params_table\">\n<tr>\n<th scope=\"col\">\u503c<\/th>\n<th scope=\"col\">\u542b\u4e49<\/th>\n<\/tr>\n<tr>\n<td>meet<\/td>\n<td>\u4fdd\u6301\u7eb5\u6a2a\u6bd4\u7f29\u653eviewBox\u9002\u5e94viewport\uff0c\u53d7<\/td>\n<\/tr>\n<tr>\n<td>slice<\/td>\n<td>\u4fdd\u6301\u7eb5\u6a2a\u6bd4\u540c\u65f6\u6bd4\u4f8b\u5c0f\u7684\u65b9\u5411\u653e\u5927\u586b\u6ee1viewport\uff0c\u653b<\/td>\n<\/tr>\n<tr>\n<td>none<\/td>\n<td>\u626d\u66f2\u7eb5\u6a2a\u6bd4\u4ee5\u5145\u5206\u9002\u5e94viewport\uff0c\u53d8\u6001<\/td>\n<\/tr>\n<\/table>\n<p>\u73b0\u5728\u6025\u9700\u4e00\u4e2a\u6d3b\u751f\u751f\u7684\u4f8b\u5b50\uff0c\u8ba9\u5927\u5bb6\u611f\u53d7\u4e0b\u8fd9\u4e09\u4e2a\u503c\u7684\u8868\u73b0\u3002<\/p>\n<p>\u60a8\u53ef\u4ee5\u72e0\u72e0\u5730\u70b9\u51fb\u8fd9\u91cc\uff1a<a class=\"a_link\" href=\"http:\/\/www.zhangxinxu.com\/study\/201408\/svg-preserveaspectratio-meet-slice-none.html\" target=\"_blank\">meet,slice,none\u529f\u80fd\u6f14\u793ademo<\/a><\/p>\n<p>\u9996\u5148\uff0c\u770b\u4e0bSVG\u4ee3\u7801\uff1a<\/p>\n<div class=\"zxx_code\">\n<pre>&lt;svg width=\"400\" height=\"200\" viewBox=\"0 0 200 200\" style=\"border:1px solid #cd0000;\">\n    &lt;rect x=\"10\" y=\"10\" width=\"150\" height=\"150\" fill=\"#cd0000\"\/>\n&lt;\/svg><\/pre>\n<\/div>\n<p>\u622a\u53d6SVG\u5de6\u8fb9\u4e00\u534a(200\u6b63\u597d\u5bbd\u5ea6400\u7684\u4e00\u822c)\u4f5c\u4e3a\u89c6\u533a\uff0c\u91cc\u9762\u6709\u4e2a<code>150*150<\/code>\u7684\u7ea2\u8272\u77e9\u5f62\u3002<\/p>\n<p>\u9ed8\u8ba4\u5c55\u793a\u5982\u4e0b\uff1a<br \/>\n<img loading=\"lazy\" decoding=\"async\" src=\"http:\/\/image.zhangxinxu.com\/image\/blog\/201408\/2014-08-27_164036.png\" width=\"482\" height=\"287\" alt=\"\u9ed8\u8ba4\u6548\u679c\" class=\"alignnone\" \/><\/p>\n<p>\u5982\u679c\u6211\u4f30\u8ba1\u6ca1\u9519\uff0c\u9ed8\u8ba4\u5e94\u8be5\u662f<code>\"xMidYmid meet\"<\/code>\u6548\u679c\u3002<\/p>\n<p><strong>\u2460 <\/strong>\u5982\u679c\u662f<code>meet<\/code>\u6548\u679c\uff0c\u4ee3\u7801\u5982\u4e0b\uff1a<\/p>\n<div class=\"zxx_code\">\n<pre>&lt;svg width=\"400\" height=\"200\" viewBox=\"0 0 200 200\" preserveAspectRatio=\"xMinYMin <span style=\"color:#cd0000;\">meet<\/span>\" style=\"border:1px solid #cd0000;\">\n    &lt;rect x=\"10\" y=\"10\" width=\"150\" height=\"150\" fill=\"#cd0000\"\/>\n&lt;\/svg><\/pre>\n<\/div>\n<p>\u622a\u56fe\u6548\u679c\u5982\u4e0b\uff1a<br \/>\n<img loading=\"lazy\" decoding=\"async\" src=\"http:\/\/image.zhangxinxu.com\/image\/blog\/201408\/2014-08-27_172010.png\" width=\"486\" height=\"302\" alt=\"meet\u6548\u679c\" class=\"alignnone\" \/><\/p>\n<p>\u8868\u73b0\u539f\u7406\u4e3a\uff1aSVG\u5bbd<code>400<\/code>, \u9ad8<code>200<\/code>\uff0cviewBox\u5bbd<code>200<\/code>, \u9ad8<code>200<\/code>. <code>x<\/code>\u6a2a\u8f74\u6bd4\u4f8b\u662f<code>2<\/code>, <code>y<\/code>\u7eb5\u8f74\u6bd4\u4f8b\u662f<code>1<\/code>. <code>meet<\/code>\u7684\u4f5c\u7528\u662f\u8ba9viewBox\u7b49\u6bd4\u4f8b\u7684\u540c\u65f6\uff0c\u5b8c\u5168\u5728SVG\u7684viewport\u4e2d\u663e\u793a\u3002\u8fd9\u91cc\uff0c\u6700\u5c0f\u6bd4\u4f8b\u662f\u7eb5\u5411\u7684<code>1<\/code>\uff0c\u6240\u4ee5\uff0c\u5b9e\u9645\u4e0aviewBox\u5e76\u6ca1\u6709\u4efb\u4f55\u7684\u7f29\u653e\u3002<\/p>\n<p>\u6211\u4eec\u53ea\u8981\u5bf9<code>viewBox<\/code>\u5c5e\u6027\u503c\u505a\u4e00\u70b9\u5c0f\u5c0f\u7684\u4fee\u6539\uff08200\u2192300\uff09\uff0c\u5c31\u53ef\u4ee5\u611f\u53d7\u5230\u7f29\u653e\u4e86\uff1a<\/p>\n<div class=\"zxx_code\">\n<pre>&lt;svg width=\"400\" height=\"200\" viewBox=\"0 0 200 <span style=\"color:#cd0000;\">300<\/span>\" preserveAspectRatio=\"xMinYMin meet\" style=\"border:1px solid #cd0000;\">\n    &lt;rect x=\"10\" y=\"10\" width=\"150\" height=\"150\" fill=\"#cd0000\"\/>\n&lt;\/svg><\/pre>\n<\/div>\n<p>\u6b64\u65f6\u7684\u663e\u793a\u6548\u679c\u4e3a\uff1a<br \/>\n<img loading=\"lazy\" decoding=\"async\" src=\"http:\/\/image.zhangxinxu.com\/image\/blog\/201408\/2014-08-27_175040.png\" width=\"473\" height=\"315\" alt=\"viewBox\u9ad8\u5ea6\u6539\u6210300\u540e\u7684\u6548\u679c\" class=\"alignnone\" \/><\/p>\n<p>\u6539\u6210<code>300<\/code>\u540e\uff0cviewBox\u7684\u9ad8\u5ea6\u5c31\u6bd4viewport\u7684<code>200<\/code>\u9ad8\uff0c\u6240\u4ee5\uff0cviewBox\u8981\u60f3\u5b8c\u5168\u9002\u5e94viewport\uff0c\u5c31\u8981\u8fdb\u884c\u7f29\u653e\uff0c\u6240\u4ee5\uff0c\u6211\u4eec\u53ef\u4ee5\u4e0a\u5230\u4e0a\u9762\u7684\u77e9\u5f62\u9762\u79ef\u53d8\u5c0f\u4e86\uff0c\u5c31\u662f\u56e0\u4e3a\u7f29\u653e\u7684\u7ed3\u679c\uff08\u7f29\u653e\u4e86<code>200\/300<\/code>, \u5dee\u4e0d\u591a\u539f\u6765\u7684<code>66.7%<\/code>\uff09\u3002<\/p>\n<p><strong>\u2461<\/strong> \u5982\u679c\u662f<code>slice<\/code>, <code>slice<\/code>\u672c\u8eab\u5c31\u6709\u526a\u5207\u7684\u610f\u601d\u3002\u4ee3\u7801\u5982\u4e0b\uff1a<\/p>\n<div class=\"zxx_code\">\n<pre>&lt;svg width=\"400\" height=\"200\" viewBox=\"0 0 200 200\" preserveAspectRatio=\"xMinYMin <span style=\"color:#cd0000;\">slice<\/span>\" style=\"border:1px solid #cd0000;\">\n    &lt;rect x=\"10\" y=\"10\" width=\"150\" height=\"150\" fill=\"#cd0000\"\/>\n&lt;\/svg><\/pre>\n<\/div>\n<p>\u6548\u679c\u622a\u56fe\uff1a<br \/>\n<img loading=\"lazy\" decoding=\"async\" src=\"http:\/\/image.zhangxinxu.com\/image\/blog\/201408\/2014-08-27_172024.png\" width=\"477\" height=\"342\" alt=\"slice\u503c\u4e0b\u7684\u6548\u679c\" class=\"alignnone\" \/><\/p>\n<p><code>slice<\/code>\u4e5f\u662f\u8981\u4fdd\u6301viewBox\u7684\u7eb5\u6a2a\u6bd4\u7684\uff0c\u4e0d\u8fc7\uff0c\u5176\u4f5c\u7528\u662f\u5c3d\u91cf\u586b\u6ee1viewport. \u540c\u6837\uff0c\u8fd9\u91ccviewBox\u5bbd\u5ea6<code>200<\/code>\uff0cSVG\u7684<code>width<\/code>\u662f<code>400<\/code>. \u663e\u7136\uff0c\u8981\u60f3\u6700\u5927\u5316\u5145\u6ee1\uff0cviewBox\u7684\u5bbd\u5ea6\u5c31\u9700\u8981\u6269\u5927\u4e3a\u539f\u6765\u7684\u4e24\u500d\u3002\u4e8e\u662f\uff0c\u5c31\u6709\u4e86\u4e0a\u56feviewBox\u653e\u5927\u4e24\u500d\u540e\u7684\u6548\u679c\u622a\u56fe\u3002\u7531\u4e8eviewBox\u90e8\u5206\u533a\u57df\u8d85\u51fa\u4e86viewport, \u89c6\u533a\u4e4b\u5916\u5185\u5bb9\u662f\u4e0d\u53ef\u89c1\u7684\uff0c\u4e8e\u662f\u5c31\u51fa\u73b0\u4e86<code>slice<\/code>\u6240\u8868\u610f\u7684\u201c\u526a\u5207\u201d\u6548\u679c\u3002<\/p>\n<p><code>\u2462<\/code> \u5982\u679c\u662f<code>none<\/code>, \u5219\u8868\u793a\u4e0d\u5173\u5fc3\u6bd4\u4f8b\uff0cviewBox\u76f4\u63a5\u62c9\u4f38\u5230\u6700\u5927\u586b\u6ee1viewport.<\/p>\n<div class=\"zxx_code\">\n<pre>&lt;svg width=\"400\" height=\"200\" viewBox=\"0 0 200 200\" preserveAspectRatio=\"<del datetime=\"2016-12-14T07:50:25+00:00\">xMinYMin <\/del><span style=\"color:#cd0000;\">none<\/span>\" style=\"border:1px solid #cd0000;\">\n    &lt;rect x=\"10\" y=\"10\" width=\"150\" height=\"150\" fill=\"#cd0000\"\/>\n&lt;\/svg><\/pre>\n<\/div>\n<p>\u7ea0\u6b63\uff0c\u76f4\u63a5\u5c5e\u6027\u503c<code>\"none\"<\/code>\u5373\u53ef\uff0c\u4f8b\u5982\uff1a<\/p>\n<div class=\"zxx_code\">\n<pre>&lt;svg width=\"400\" height=\"200\" viewBox=\"0 0 200 200\" preserveAspectRatio=\"<span style=\"color:#cd0000;\">none<\/span>\" style=\"border:1px solid #cd0000;\">\n    &lt;rect x=\"10\" y=\"10\" width=\"150\" height=\"150\" fill=\"#cd0000\"\/>\n&lt;\/svg><\/pre>\n<\/div>\n<p>\u9ed8\u8ba4\u72b6\u6001\u4e0b\uff0c\u4ee5\u53ca\u4f7f\u7528\u5de5\u5177\u5bfc\u51fa\u7684SVG\u56fe\u5f62\u5b50\u554a\u8bbe\u7f6e\u5bbd\u9ad8\u7684\u65f6\u5019\u90fd\u662f\u4fdd\u6301\u6bd4\u4f8b\u4e0d\u62c9\u4f38\u7684\uff0c\u8fd9\u5bf9\u4e8e\u56fe\u6807\u7b49\u5c3a\u5bf8\u63a7\u5236\u975e\u5e38\u6709\u7528\uff0c\u4f46\u5982\u679c\u6211\u4eec\u5e0c\u671bSVG\u56fe\u5f62\u50cf\u56fe\u7247\u9ed8\u8ba4\u884c\u4e3a\u4e00\u6837\uff0c\u5bbd\u9ad8\u8bbe\u7f6e\u76f4\u63a5\u62c9\u4f38\uff0c\u5c31\u662f\u8bbe\u7f6e\u8fd9\u91cc\u7684<code>preserveAspectRatio=\"none\"<\/code>\u5373\u53ef\uff01<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" src=\"http:\/\/image.zhangxinxu.com\/image\/blog\/201408\/2014-08-27_172106.png\" width=\"489\" height=\"376\" alt=\"none\u65f6\u5019\u7684\u62c9\u4f38\u586b\u5145\u6548\u679c\" class=\"alignnone\" \/><\/p>\n<p>\u539f\u672c\u597d\u597d\u7684\u4e00\u4e2a\u6b63\u65b9\u5f62\uff0c\u73b0\u5728\u56e0\u4e3aviewBox\u7684\u62c9\u4f38\uff0c\u53d8\u6210\u4e86\u4e00\u4e2a\u5bbd\u9ad8<code>2:1<\/code>\u7684\u77e9\u5f62\u4e86\u3002<\/p>\n<p><strong>viewBox\u7684\u5bf9\u9f50<\/strong><br \/>\n\u5343\u8a00\u4e07\u8bed\u4e0d\u5982\u4e00\u4e2a\u53ef\u4ee5\u81ea\u5df1\u52a8\u624b\u4f53\u9a8c\u7684demo\u5b9e\u5728\uff0c\u60a8\u53ef\u4ee5\u72e0\u72e0\u5730\u70b9\u51fb\u8fd9\u91cc\uff1a<a class=\"a_link\" href=\"http:\/\/www.zhangxinxu.com\/study\/201408\/svg-viewbox-alignment.html\" target=\"_blank\">viewBox\u7684\u5bf9\u9f50\u5404\u4e2a\u540d\u79f0\u8868\u73b0\u611f\u53d7demo<\/a><\/p>\n<p>\u622a\u4e24\u5f20\u56fe\u7ed9\u5927\u5bb6\u7785\u7785\uff1a<br \/>\n<img loading=\"lazy\" decoding=\"async\" src=\"http:\/\/image.zhangxinxu.com\/image\/blog\/201408\/2014-08-27_183555.png\" width=\"456\" height=\"379\" alt=\"viewBox\u5bf9\u9f50demo\u7684\u622a\u56fe\" class=\"alignnone\" \/><br \/>\n<img loading=\"lazy\" decoding=\"async\" src=\"http:\/\/image.zhangxinxu.com\/image\/blog\/201408\/2014-08-27_183645.png\" width=\"480\" height=\"387\" alt=\"viewBox\u5bf9\u9f50\u6f14\u793ademo\u7684\u622a\u56fe\" class=\"alignnone\" \/><\/p>\n<p>\u65e0\u8bba\u662f<code>meet<\/code>\u8fd8\u662f<code>slice<\/code>\uff0c\u4f60\u662f\u4e0d\u53ef\u80fd\u5728\u4e00\u79cd\u72b6\u6001\u4e0b\u540c\u65f6\u770b\u5230<code>x<\/code>, <code>y<\/code>\u65b9\u5411\u4e0a\u7684\u4f4d\u79fb\u7684\u3002\u56e0\u4e3a\u603b\u4f1a\u6709\u4e00\u4e2a\u65b9\u5411\u662f\u5145\u6ee1viewport\u7684\u3002<img decoding=\"async\" src=\"http:\/\/mat1.gtimg.com\/www\/mb\/images\/face\/14.gif\" align=\"absmiddle\"><\/p>\n<h3>\u7ed3\u675f\u8bed<\/h3>\n<p>\u672c\u6587\u662f\u51e0\u4e4e\u6ca1\u6709\u4e2a\u4eba\u60c5\u7eea\uff0c\u4e2a\u4eba\u5410\u69fd\u7684\u4e00\u7bc7\u57fa\u7840\u6280\u672f\u6587\u7ae0\uff0c\u4ee5\u6ee1\u8db3\u4e0d\u540c\u7fa4\u4f53\u7684\u80c3\u53e3\u3002<\/p>\n<p>\u884c\u6587\u4ed3\u4fc3\uff0c\u9519\u8bef\u96be\u514d\uff0c\u6b22\u8fce\u7ea0\u9519\u3002\u6b22\u8fce\u4ea4\u6d41\u3002<\/p>\n<p>\u884c\u6587\u53c2\u8003\uff1a<a href=\"http:\/\/tutorials.jenkov.com\/svg\/svg-viewport-view-box.html\">SVG Viewport and View Box<\/a><\/p>\n<p>\u672c\u6587\u4e3a\u539f\u521b\u6587\u7ae0\uff0c\u4f1a\u7ecf\u5e38\u66f4\u65b0\u77e5\u8bc6\u70b9\u4ee5\u53ca\u4fee\u6b63\u4e00\u4e9b\u9519\u8bef\uff0c\u56e0\u6b64\u8f6c\u8f7d\u8bf7\u4fdd\u7559\u539f\u51fa\u5904\uff0c\u65b9\u4fbf\u6eaf\u6e90\uff0c\u907f\u514d\u9648\u65e7\u9519\u8bef\u77e5\u8bc6\u7684\u8bef\u5bfc\uff0c\u540c\u65f6\u6709\u66f4\u597d\u7684\u9605\u8bfb\u4f53\u9a8c\u3002<br \/>\n\u672c\u6587\u5730\u5740\uff1a<a href=\"http:\/\/www.zhangxinxu.com\/wordpress\/?p=4323\">http:\/\/www.zhangxinxu.com\/wordpress\/?p=4323<\/a><\/p>\n<p>\uff08\u672c\u7bc7\u5b8c\uff09<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u672c\u6587\u5185\u5bb9\u5e73\u5b9e\uff0c\u5c31\u662f\u6807\u9898\u6240\u63d0\u5230\u7684\u4e09\u4e2a\u4e1c\u897f\uff0cviewport,viewBox,preserveAspectRatio, \u90fd\u662f\u5b66\u4e60SVG\u5fc5\u987b\u638c\u63e1\u7684\u57fa\u7840\u77e5\u8bc6\u3002\u4e00\u5982\u65e2\u5f80\uff0c\u6709demo\uff0c\u6709\u622a\u56fe\uff0c\u6709\u6e90\u4ee3\u7801\u5c55\u793a\u3002\u5e0c\u671b\u672c\u6587\u7684\u5185\u5bb9\u80fd\u591f\u5bf9\u60a8\u7684\u5b66\u4e60\u6709\u6240\u5e2e\u52a9\u3002<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[895],"tags":[943,642,942,941,751],"_links":{"self":[{"href":"https:\/\/www.zhangxinxu.com\/wordpress\/wp-json\/wp\/v2\/posts\/4323"}],"collection":[{"href":"https:\/\/www.zhangxinxu.com\/wordpress\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.zhangxinxu.com\/wordpress\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.zhangxinxu.com\/wordpress\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.zhangxinxu.com\/wordpress\/wp-json\/wp\/v2\/comments?post=4323"}],"version-history":[{"count":0,"href":"https:\/\/www.zhangxinxu.com\/wordpress\/wp-json\/wp\/v2\/posts\/4323\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.zhangxinxu.com\/wordpress\/wp-json\/wp\/v2\/media?parent=4323"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.zhangxinxu.com\/wordpress\/wp-json\/wp\/v2\/categories?post=4323"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.zhangxinxu.com\/wordpress\/wp-json\/wp\/v2\/tags?post=4323"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}