REM自適應頁面佈局

注意
毫不是每一個地方都要用rem,rem只適用於固定尺寸!
在至關數量的佈局情境中(好比底部導航元素平分屏幕寬,大尺寸元素),你必須使用百分比或者flex才能完美佈局!

經常使用手機尺寸
iphone5 320 * 2 = 640px
iphone6 375 * 2 = 750px
iphone6s 414 * 3 = 1242px

 問:高清方案在微信上,有時候字體會不受控制變的很大,怎麼辦?
問題緣由:
在X5新內核Blink中,在排版頁面的時候,會主動對字體進行放大,會檢測頁面中的主字體,當某一塊字體在
咱們的斷定規則中,認爲字號較小,而且是頁面中的主要字體,就會採起主動放大的操做。然而這不是咱們想要的,能夠採起給最大高度解決
解決方案:微信

*, *:before, *:after { max-height: 100000px }


- 後續:通過項目實踐,仍是決定給 max-height 一個具體數值比較好,以前給的是 100% ,但有自身的侷限性,好比 antd 的輪播組件在 max-height:100% 的狀況下就不能正常顯示。
- 補充:有同窗反映,在一些狀況下 textarea 標籤內的字體大小即使加上上面的方案,字體也會變大,沒法控制。此時你須要給 textarea 的 display 設爲 table 或者 inline-table 便可恢復正常。(感謝 程序媛喵喵 對此的補充)


源碼解讀antd

~function () {
            var winW = document.documentElement.clientWidth,
                    desW = 640;
            if (winW < desW) {/*超過設計稿,最大就按照設計稿的尺寸便可*/
                document.documentElement.style.fontSize = winW / desW * 100 + 'px';
            }
        }();

- 基本適合任何移動設備app

'use strict';

/**
 * @param {Number} [baseFontSize = 100] - 基礎fontSize, 默認100px;
 * @param {Number} [fontscale = 1] - 有的業務但願能放大必定比例的字體;
 */
const win = window;
export default win.flex = (baseFontSize, fontscale) => {
  const _baseFontSize = baseFontSize || 100;
  const _fontscale = fontscale || 1;

  const doc = win.document;
  const ua = navigator.userAgent;
  const matches = ua.match(/Android[\S\s]+AppleWebkit\/(\d{3})/i);
  const UCversion = ua.match(/U3\/((\d+|\.){5,})/i);
  const isUCHd = UCversion && parseInt(UCversion[1].split('.').join(''), 10) >= 80;
  const isIos = navigator.appVersion.match(/(iphone|ipad|ipod)/gi);
  let dpr = win.devicePixelRatio || 1;
  if (!isIos && !(matches && matches[1] > 534) && !isUCHd) {
    // 若是非iOS, 非Android4.3以上, 非UC內核, 就不執行高清, dpr設爲1;
    dpr = 1;
  }
  const scale = 1 / dpr;

  let metaEl = doc.querySelector('meta[name="viewport"]');
  if (!metaEl) {
    metaEl = doc.createElement('meta');
    metaEl.setAttribute('name', 'viewport');
    doc.head.appendChild(metaEl);
  }
  metaEl.setAttribute('content', `width=device-width,user-scalable=no,initial-scale=${scale},maximum-scale=${scale},minimum-scale=${scale}`);
  doc.documentElement.style.fontSize = `${_baseFontSize / 2 * dpr * _fontscale}px`;
};
相關文章
相關標籤/搜索