{"id":2318,"date":"2017-11-11T14:25:10","date_gmt":"2017-11-11T06:25:10","guid":{"rendered":"http:\/\/www.xttblog.com\/?p=2318"},"modified":"2017-11-11T14:25:11","modified_gmt":"2017-11-11T06:25:11","slug":"js%e5%ae%9e%e7%8e%b0rem%e8%87%aa%e5%8a%a8%e5%8c%b9%e9%85%8d%e8%ae%a1%e7%ae%97font-size","status":"publish","type":"post","link":"https:\/\/www.xttblog.com\/?p=2318","title":{"rendered":"js\u5b9e\u73b0rem\u81ea\u52a8\u5339\u914d\u8ba1\u7b97font-size"},"content":{"rendered":"<p>\n\t\u5b9e\u9645\u5f00\u53d1\u8fc7\u7a0b\u4e2d\uff0c\u6211\u4eec\u7ecf\u5e38\u4f1a\u88ab\u5404\u79cd\u5bbd\u5ea6\uff0c\u9ad8\u5ea6\u8ba1\u7b97\u641e\u6655\u3002\u5c24\u5176\u662f\u4f7f\u7528\u4e86rem\u7684\u8ba1\u7b97\u65b9\u5f0f\uff0c\u81ea\u9002\u5e94\u5e03\u5c40\u96be\u5012\u4e00\u5927\u7247\u7a0b\u5e8f\u5458\u3002\u4e3a\u4e86\u89e3\u51b3\u8fd9\u7c7b\u95ee\u9898\uff0c\u6211\u89c9\u5f97\u53ef\u4ee5\u5229\u7528js\u76d1\u542c\u5c4f\u5e55\u5bbd\u5ea6\u53d8\u5316\u6765\u5b9e\u73b0\u66f4\u6539HTML \u6839\u5143\u7d20font-size\u7684\u503c\u3002\n<\/p>\n<p>\n\t\u4e0b\u9762\u662f\u76f8\u5173JavaScript\u7684\u5b9e\u73b0\u4ee3\u7801\uff1a\n<\/p>\n<pre class=\"brush:jscript;\">\r\n(function (doc, win) {\r\n\tvar docEl = doc.documentElement,\r\n\t\tresizeEvt = &#039;orientationchange&#039; in window ? &#039;orientationchange&#039; : &#039;resize&#039;,\r\n\t\trecalc = function () {\r\n\t\t\tvar clientWidth = docEl.clientWidth;\r\n\t\t\tif (!clientWidth) return;\r\n\t\t\tif(clientWidth&gt;=640){\r\n\t\t\t\tdocEl.style.fontSize = &#039;100px&#039;;\r\n\t\t\t}else{\r\n\t\t\t\tdocEl.style.fontSize = 100 * (clientWidth \/ 640) + &#039;px&#039;;\r\n\t\t\t}\r\n\t\t};\r\n\t\/\/ \u4e1a\u4f59\u8349\uff1awww.xttblog.com\r\n\tif (!doc.addEventListener) return;\r\n\twin.addEventListener(resizeEvt, recalc, false);\r\n\tdoc.addEventListener(&#039;DOMContentLoaded&#039;, recalc, false);\r\n})(document, window);<\/pre>\n<p>\n\t\u6b64\u4ee3\u7801\u9009640px\u4e3a\u57fa\u51c6\u503c\uff0c\u4e3a\u4ec0\u4e48\u9009640\u5462\uff0c\n<\/p>\n<p>\n\t640px\u7684\u9875\u9762\u5bbd\u5ea6\u662f\u4e00\u4e2a\u5b89\u5168\u7684\u6700\u5927\u5bbd\u5ea6\uff0c\u4fdd\u8bc1\u4e86\u79fb\u52a8\u7aef\u9875\u9762\u4e24\u8fb9\u4e0d\u4f1a\u7559\u767d\u3002\u6ce8\u610f\u8fd9\u91cc\u7684px\u662fcss\u903b\u8f91\u50cf\u7d20\uff0c\u4e0e\u8bbe\u5907\u7684\u7269\u7406\u50cf\u7d20\u662f\u6709\u533a\u522b\u7684\u3002\u5982iPhone 5\u4f7f\u7528\u7684\u662fRetina\u89c6\u7f51\u819c\u5c4f\u5e55\uff0c\u4f7f\u75282px x 2px\u7684 device pixel \u4ee3\u8868 1px x 1px \u7684 css pixel\uff0c\u6240\u4ee5\u8bbe\u5907\u50cf\u7d20\u6570\u4e3a640 x 1136px\uff0c\u800c\u5b83\u7684CSS\u903b\u8f91\u50cf\u7d20\u6570\u4e3a320 x 568px\u3002<br \/>\n\t\u6240\u4ee5\u5f53\u8981\u5207\u79fb\u52a8\u7aef\u7684\u9875\u9762\u7684\u65f6\u5019\uff0c\u9700\u8981\u628a\u6548\u679c\u56fe\u5bbd\u5ea6\u7b49\u6bd4\u4f8b\u7f29\u653e\u5230640px\u3002\n<\/p>\n<p>\n\t\u6bd4\u5982\u5f53\u9875\u9762\u4e2d\u67d0\u4e00div\u7684\u5bbd\u5ea6\u4e3a60\uff0c\u9ad8\u5ea6\u4e3a65\u7684\u65f6\u5019\uff0c\u5c31\u53ef\u4ee5\u76f4\u63a5\u8fd9\u6837\u5199\u6837\u5f0f\uff1a\n<\/p>\n<pre class=\"brush:css;\">\r\n{\r\n    width:0.6rem;\r\n    height:0.65rem\r\n}<\/pre>\n<h2>\n\t\u6d4f\u89c8\u5668\u7684\u517c\u5bb9\u6027<br \/>\n<\/h2>\n<p>\n\t<a href=\"http:\/\/www.w3.org\/TR\/css3-values\/#rem-unit\" target=\"_blank\">rem<\/a>\u662f<a href=\"http:\/\/www.w3.org\/TR\/css3-values\/\" target=\"_blank\">CSS3<\/a>\u65b0\u5f15\u8fdb\u6765\u7684\u4e00\u4e2a\u5ea6\u91cf\u5355\u4f4d\uff0c\u5927\u5bb6\u5fc3\u91cc\u80af\u5b9a\u4f1a\u89c9\u5f97\u5fc3\u7070\u610f\u51b7\u5440\uff0c\u62c5\u5fc3\u6d4f\u89c8\u5668\u7684\u652f\u6301\u60c5\u51b5\u3002\u5176\u5b9e\u5927\u5bb6\u4e0d\u7528\u5bb3\u6015\uff0c\u4f60\u53ef\u80fd\u4f1a\u60ca\u8bb6\uff0c\u652f\u6301\u7684\u6d4f\u89c8\u5668\u8fd8\u662f\u86ee\u591a\u7684\uff0c\u6bd4\u5982\uff1a<a href=\"http:\/\/firefox.com\/\" target=\"_blank\">Mozilla Firefox 3.6+<\/a>\u3001<a href=\"http:\/\/apple.com\/safari\/\" target=\"_blank\">Apple Safari 5+<\/a>\u3001<a href=\"http:\/\/google.com\/chrome\" target=\"_blank\">Google Chrome<\/a>\u3001<a href=\"http:\/\/windows.microsoft.com\/en-US\/internet-explorer\/products\/ie\/home\" target=\"_blank\">IE9+<\/a>\u548c<a href=\"http:\/\/www.opera.com\/\" target=\"_blank\">Opera11+<\/a>\u3002\u53ea\u662f\u53ef\u601c\u7684IE6-8\u65e0\u6cd5\uff0c\u4f60\u4eec\u5c31\u628a\u4ed6\u4eec\u5f53\u900f\u660e\u4e86\u5427\uff0c\u6211\u5411\u6765\u90fd\u662f\u5982\u6b64\u3002\n<\/p>\n<p>\n\t\u4e0d\u8fc7\u4f7f\u7528\u5355\u4f4d\u8bbe\u7f6e\u5b57\u4f53\uff0c\u53ef\u4e0d\u80fd\u5b8c\u5168\u4e0d\u8003\u8651IE\u4e86\uff0c\u5982\u679c\u4f60\u60f3\u4f7f\u7528\u8fd9\u4e2aREM\uff0c\u4f46\u4e5f\u60f3\u517c\u5bb9IE\u4e0b\u7684\u6548\u679c\uff0c\u53ef\u4f60\u53ef\u8003\u8651&ldquo;px&rdquo;\u548c&ldquo;rem&rdquo;\u4e00\u8d77\u4f7f\u7528\uff0c\u7528&quot;px&quot;\u6765\u5b9e\u73b0IE6-8\u4e0b\u7684\u6548\u679c\uff0c\u7136\u540e\u4f7f\u7528&ldquo;Rem&rdquo;\u6765\u5b9e\u73b0\u4ee3\u6d4f\u89c8\u5668\u7684\u6548\u679c\u3002\u5c31\u8ba9IE6-8\u4e0d\u80fd\u968f\u6587\u5b57\u7684\u6539\u53d8\u800c\u6539\u53d8\u5427\uff0c\u8c01\u8ba9\u8fd9\u4e2aIe6-8\u8fd9\u4e48\u8001\u5462\uff1f\u5927\u5bb6\u4e0d\u4eff\u8bd5\u8bd5\uff0c\u8fd8\u86ee\u6709\u610f\u601d\uff0c\u8bf4\u4e0d\u5b9a\u8fd9\u4e2a\u5c31\u662f\u4e3b\u6d41\u7684\u5ea6\u91cf\u5355\u4f4d\u4e86\u3002<\/p>\n<!-- <audio autoplay=\"autoplay\" height=\"100\" width=\"100\" hidden=\"true\"><source src=\"https:\/\/c.coka.la\/orAvPR.mp3\" type=\"audio\/mp3\"\/><\/audio><img decoding=\"async\" src=\"https:\/\/images.weserv.nl\/?url=https:\/\/ws3.sinaimg.cn\/large\/8fa5dcfcgy1g4em3rzar5j20ib089ae0.jpg\" alt=\"\u4e1a\u4f59\u8349\u516c\u4f17\u53f7\"\/> --><p><!-- <img decoding=\"async\" src=\"https:\/\/raw.githubusercontent.com\/xmt1139057136\/xttblog\/master\/8fa5dcfcgy1g4em3rzar5j20ib089ae0.jpg\" alt=\"\u4e1a\u4f59\u8349\u516c\u4f17\u53f7\"\/>--><img decoding=\"async\" src=\"https:\/\/saas.51autoparts.cn\/api\/admin\/sys-file\/oss\/file?fileName=84db51f8561e448f9a4fda2eff0811f9.jpg\" alt=\"\u4e1a\u4f59\u8349\u516c\u4f17\u53f7\"\/><\/p><p>\u6700\u540e\uff0c\u6b22\u8fce\u5173\u6ce8\u6211\u7684\u4e2a\u4eba\u5fae\u4fe1\u516c\u4f17\u53f7\uff1a\u4e1a\u4f59\u8349\uff08yyucao\uff09\uff01<b>\u53ef\u52a0\u4f5c\u8005\u5fae\u4fe1\u53f7\uff1a<font color=\"red\">xttblog2<\/font>\u3002\u5907\u6ce8\uff1a\u201c1\u201d\uff0c\u6dfb\u52a0\u535a\u4e3b\u5fae\u4fe1\u62c9\u4f60\u8fdb\u5fae\u4fe1\u7fa4\u3002\u5907\u6ce8\u9519\u8bef\u4e0d\u4f1a\u540c\u610f\u597d\u53cb\u7533\u8bf7\u3002<\/b>\u518d\u6b21\u611f\u8c22\u60a8\u7684\u5173\u6ce8\uff01\u540e\u7eed\u6709\u7cbe\u5f69\u5185\u5bb9\u4f1a\u7b2c\u4e00\u65f6\u95f4\u53d1\u7ed9\u60a8\uff01\u539f\u521b\u6587\u7ae0\u6295\u7a3f\u8bf7\u53d1\u9001\u81f3532009913@qq.com\u90ae\u7bb1\u3002\u5546\u52a1\u5408\u4f5c\u4e5f\u53ef\u6dfb\u52a0\u4f5c\u8005\u5fae\u4fe1\u8fdb\u884c\u8054\u7cfb\uff01<\/p><p>\u672c\u6587\u539f\u6587\u51fa\u5904\uff1a<a href=\"https:\/\/www.xttblog.com\">\u4e1a\u4f59\u8349<\/a>\uff1a &raquo; <a href=\"https:\/\/www.xttblog.com\/?p=2318\">js\u5b9e\u73b0rem\u81ea\u52a8\u5339\u914d\u8ba1\u7b97font-size<\/a><\/p><!-- <div class=\"bd-reward-stl\"><button id=\"bdRewardBtn\"><span><\/span><\/button><\/div> -->","protected":false},"excerpt":{"rendered":"<p>\u5b9e\u9645\u5f00\u53d1\u8fc7\u7a0b\u4e2d\uff0c\u6211\u4eec\u7ecf\u5e38\u4f1a\u88ab\u5404\u79cd\u5bbd\u5ea6\uff0c\u9ad8\u5ea6\u8ba1\u7b97\u641e\u6655\u3002\u5c24\u5176\u662f\u4f7f\u7528\u4e86rem\u7684\u8ba1\u7b97\u65b9\u5f0f\uff0c\u81ea\u9002\u5e94\u5e03\u5c40\u96be\u5012\u4e00\u5927\u7247\u7a0b\u5e8f\u5458\u3002 [&hellip;]<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[4],"tags":[814,119,1106],"class_list":["post-2318","post","type-post","status-publish","format-standard","hentry","category-html5","tag-css3","tag-html5","tag-javascript"],"_links":{"self":[{"href":"https:\/\/www.xttblog.com\/index.php?rest_route=\/wp\/v2\/posts\/2318","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.xttblog.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.xttblog.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.xttblog.com\/index.php?rest_route=\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/www.xttblog.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=2318"}],"version-history":[{"count":1,"href":"https:\/\/www.xttblog.com\/index.php?rest_route=\/wp\/v2\/posts\/2318\/revisions"}],"predecessor-version":[{"id":2319,"href":"https:\/\/www.xttblog.com\/index.php?rest_route=\/wp\/v2\/posts\/2318\/revisions\/2319"}],"wp:attachment":[{"href":"https:\/\/www.xttblog.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=2318"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.xttblog.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=2318"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.xttblog.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=2318"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}