rem由來:font size of the root element,那麼rem是個單位,單位大小由它第一代老祖宗的font-size的大小決定。現在前端碼農們為了能在各個螢幕上看到一個健康的網頁在默默的犧牲著自己的健康,因為不僅要知道rem是個單位,更重要的是要知道怎麼能在不同分辨率下呈現的頁面都很NB。
事故造成原因:
1.px單位在PC上很流行,在手機螢幕上一看,MLGB的,同樣的12px卻小的跟螞蟻似的。
2.好不容易在iPhone4上調的正常了,換個菊花牌手機,MBD不堪入目了。
3.知道了rem的用法,但是html的font-size到底是多少才合適啊啊啊,媽蛋~。
好了,那麼現在來解決這些問題。
在解決之前,麻煩各位大嬸要了解一些你可能不想了解的東東(警告:不了解這些就不能知道真相喲~):
1 .物理像素(physical pixel)
我們看到的每個螢幕都是由一顆顆我們肉眼難以看到的小顆粒(物理像素)組成的。
2.邏輯像素
是電腦座標系統中的一個點,這個點代表一個可以由程式使用的虛擬像素(比如說CSS像素)。
3.裝置的像素比(device pixel ratio)簡稱DPR
它的數值體現了實體像素和邏輯像素之間的關係,用公式可以計算出該設備的DPR的大小:
DPR = 物理像素 / 逻辑像素
#那麼了解上面這些概念,就可以知道,為什麼css在pc上寫著font-size=12px;
但換到手機上卻變小了?因為DPR啊啊啊,大哥~。
沒錯,我們在電腦螢幕上的DPR是1,但手機卻不同,可能是它可能是2,也可能是3。取得裝置DPR的方法還是有的:
1.在JavaScript中,透過window.devicePixelRatio
來取得
2.在css中,可以透過-webkit-device-pixel-ratio
,-webkit-min-device-pixel-ratio
和 -webkit-max-device-pixel-ratio
進行媒體查詢,對不同DPR的設備,做一些樣式適配(這裡只針對webkit核心的瀏覽器和webview)。
本人也在網路上看了不少動態設定rem的文章,下面把幾個常用的列舉出來:
#一,用媒體查詢設定html的font-size:
@media screen and (min-width: 320px) {html {font-size: 14px;} } @media screen and (min-width: 360px) {html {font-size: 16px;} } @media screen and (min-width: 400px) {html {font-size: 18px;} } @media screen and (min-width: 440px) {html {font-size: 20px;} } @media screen and (min-width: 480px) {html {font-size: 22px;} } @media screen and (min-width: 640px) {html {font-size: 28px;} }
#二、利用js來動態設定
!(function(doc, win) {var docEle = doc.documentElement, evt = "onorientationchange" in window ? "orientationchange" : "resize", fn = function() {var width = docEle.clientWidth; width && (docEle.style.fontSize = 20 * (width / 320) + "px"); }; win.addEventListener(evt, fn, false); doc.addEventListener("DOMContentLoaded", fn, false); }(document, window));
我要說的是最後一種,也是我認為目前比較好的實作方法:
利用js計算目前裝置的DPR,動態設定在html標籤上,並動態設定html的 font-size
,利用css的選擇器根據DPR來設定不同DPR下的字體大小(這個方法很不錯哦~)
<span style="font-size: 18px"><code>!<span class="hljs-function"><span class="hljs-keyword">function(<span class="hljs-params">win, lib) {<span class="hljs-keyword">var timer, doc = win.document, docElem = doc.documentElement, vpMeta = doc.querySelector(<span class="hljs-string">'meta[name="viewport"]'), flexMeta = doc.querySelector(<span class="hljs-string">'meta[name="flexible"]'), dpr = <span class="hljs-number">0, scale = <span class="hljs-number">0, flexible = lib.flexible || (lib.flexible = {}); <span class="hljs-comment">// 设置了 viewport meta<span class="hljs-keyword">if (vpMeta) { <span class="hljs-built_in">console.warn(<span class="hljs-string">"将根据已有的meta标签来设置缩放比例");<span class="hljs-keyword">var initial = vpMeta.getAttribute(<span class="hljs-string">"content").match(<span class="hljs-regexp">/initial\-scale=([\d\.]+)/); <span class="hljs-keyword">if (initial) { scale = <span class="hljs-built_in">parseFloat(initial[<span class="hljs-number">1]); <span class="hljs-comment">// 已设置的 initialScale dpr = <span class="hljs-built_in">parseInt(<span class="hljs-number">1 / scale); <span class="hljs-comment">// 设备像素比 devicePixelRatio } }<span class="hljs-comment">// 设置了 flexible Meta<span class="hljs-keyword">else <span class="hljs-keyword">if (flexMeta) {<span class="hljs-keyword">var flexMetaContent = flexMeta.getAttribute(<span class="hljs-string">"content");<span class="hljs-keyword">if (flexMetaContent) { <span class="hljs-keyword">var initial = flexMetaContent.match(<span class="hljs-regexp">/initial\-dpr=([\d\.]+)/), maximum = flexMetaContent.match(<span class="hljs-regexp">/maximum\-dpr=([\d\.]+)/); <span class="hljs-keyword">if (initial) { dpr = <span class="hljs-built_in">parseFloat(initial[<span class="hljs-number">1]); scale = <span class="hljs-built_in">parseFloat((<span class="hljs-number">1 / dpr).toFixed(<span class="hljs-number">2)); } <span class="hljs-keyword">if (maximum) { dpr = <span class="hljs-built_in">parseFloat(maximum[<span class="hljs-number">1]); scale = <span class="hljs-built_in">parseFloat((<span class="hljs-number">1 / dpr).toFixed(<span class="hljs-number">2)); } } } <span class="hljs-comment">// viewport 或 flexible<span class="hljs-comment">// meta 均未设置<span class="hljs-keyword">if (!dpr && !scale) {<span class="hljs-comment">// QST<span class="hljs-comment">// 这里的 第一句有什么用 ?<span class="hljs-comment">// 和 Android 有毛关系 ?<span class="hljs-keyword">var u = (win.navigator.appVersion.match(<span class="hljs-regexp">/android/gi), win.navigator.appVersion.match(<span class="hljs-regexp">/iphone/gi)), _dpr = win.devicePixelRatio; <span class="hljs-comment">// 所以这里似乎是将所有 Android 设备都设置为 1 了 dpr = u ? ( (_dpr >= <span class="hljs-number">3 && (!dpr || dpr >= <span class="hljs-number">3)) ? <span class="hljs-number">3 : (_dpr >= <span class="hljs-number">2 && (!dpr || dpr >= <span class="hljs-number">2)) ? <span class="hljs-number">2 : <span class="hljs-number">1 ) : <span class="hljs-number">1; scale = <span class="hljs-number">1 / dpr; } docElem.setAttribute(<span class="hljs-string">"data-dpr", dpr); <span class="hljs-comment">// 插入 viewport meta<span class="hljs-keyword">if (!vpMeta) { vpMeta = doc.createElement(<span class="hljs-string">"meta"); vpMeta.setAttribute(<span class="hljs-string">"name", <span class="hljs-string">"viewport"); vpMeta.setAttribute(<span class="hljs-string">"content",<span class="hljs-string">"initial-scale=" + scale + <span class="hljs-string">", maximum-scale=" + scale + <span class="hljs-string">", minimum-scale=" + scale + <span class="hljs-string">", user-scalable=no"); <span class="hljs-keyword">if (docElem.firstElementChild) { docElem.firstElementChild.appendChild(vpMeta) } <span class="hljs-keyword">else {<span class="hljs-keyword">var div = doc.createElement(<span class="hljs-string">"div"); div.appendChild(vpMeta); doc.write(div.innerHTML); } } <span class="hljs-function"><span class="hljs-keyword">function <span class="hljs-title">setFontSize(<span class="hljs-params">) {<span class="hljs-keyword">var winWidth = docElem.getBoundingClientRect().width; <span class="hljs-keyword">if (winWidth / dpr > <span class="hljs-number">540) { (winWidth = <span class="hljs-number">540 * dpr); } <span class="hljs-comment">// 根节点 fontSize 根据宽度决定<span class="hljs-keyword">var baseSize = winWidth / <span class="hljs-number">10; docElem.style.fontSize = baseSize + <span class="hljs-string">"px"; flexible.rem = win.rem = baseSize; } <span class="hljs-comment">// 调整窗口时重置 win.addEventListener(<span class="hljs-string">"resize", <span class="hljs-function"><span class="hljs-keyword">function(<span class="hljs-params">) { clearTimeout(timer); timer = setTimeout(setFontSize, <span class="hljs-number">300); }, <span class="hljs-literal">false); <span class="hljs-comment">// 这一段是我自己加的<span class="hljs-comment">// orientationchange 时也需要重算下吧 win.addEventListener(<span class="hljs-string">"orientationchange", <span class="hljs-function"><span class="hljs-keyword">function(<span class="hljs-params">) { clearTimeout(timer); timer = setTimeout(setFontSize, <span class="hljs-number">300); }, <span class="hljs-literal">false); <span class="hljs-comment">// pageshow<span class="hljs-comment">// keyword: 倒退 缓存相关 win.addEventListener(<span class="hljs-string">"pageshow", <span class="hljs-function"><span class="hljs-keyword">function(<span class="hljs-params">e) {<span class="hljs-keyword">if (e.persisted) { clearTimeout(timer); timer = setTimeout(setFontSize, <span class="hljs-number">300); } }, <span class="hljs-literal">false); <span class="hljs-comment">// 设置基准字体<span class="hljs-keyword">if (<span class="hljs-string">"complete" === doc.readyState) { doc.body.style.fontSize = <span class="hljs-number">12 * dpr + <span class="hljs-string">"px"; } <span class="hljs-keyword">else { doc.addEventListener(<span class="hljs-string">"DOMContentLoaded", <span class="hljs-function"><span class="hljs-keyword">function(<span class="hljs-params">) { doc.body.style.fontSize = <span class="hljs-number">12 * dpr + <span class="hljs-string">"px"; }, <span class="hljs-literal">false); } setFontSize(); flexible.dpr = win.dpr = dpr; flexible.refreshRem = setFontSize; flexible.rem2px = <span class="hljs-function"><span class="hljs-keyword">function(<span class="hljs-params">d) {<span class="hljs-keyword">var c = <span class="hljs-built_in">parseFloat(d) * <span class="hljs-keyword">this.rem;<span class="hljs-keyword">if (<span class="hljs-string">"string" == <span class="hljs-keyword">typeof d && d.match(<span class="hljs-regexp">/rem$/)) { c += <span class="hljs-string">"px"; }<span class="hljs-keyword">return c; }; flexible.px2rem = <span class="hljs-function"><span class="hljs-keyword">function(<span class="hljs-params">d) {<span class="hljs-keyword">var c = <span class="hljs-built_in">parseFloat(d) / <span class="hljs-keyword">this.rem; <span class="hljs-keyword">if (<span class="hljs-string">"string" == <span class="hljs-keyword">typeof d && d.match(<span class="hljs-regexp">/px$/)) { c += <span class="hljs-string">"rem"; }<span class="hljs-keyword">return c; } }(<span class="hljs-built_in">window, <span class="hljs-built_in">window.lib || (<span class="hljs-built_in">window.lib = {}));<br/><br/></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></code>手机淘宝很多页面用的就是这种方法来适配终端的。<br/><br/><br/><br/></span>
以上是行動端動態設定字體大小的實例詳解的詳細內容。更多資訊請關注PHP中文網其他相關文章!