移動端之js控制rem,適配字型

來源:互聯網
上載者:User

標籤:lis   color   手機瀏覽器   client   get   ret   支援   cti   blog   

方法一:設定fontsize 按照iphone 5的適配  1em=10px    適配320

// “()()”表示自執行函數(function (doc, win) {  var docEl = doc.documentElement,    // 手機旋轉事件,大部分手機瀏覽器都支援 onorientationchange 如果不支援,可以使用原始的 resize      resizeEvt = ‘orientationchange‘ in window ? ‘orientationchange‘ : ‘resize‘,      recalc = function () {        //clientWidth: 擷取對象可見內容的寬度,不包括捲軸,不包括邊框        var clientWidth = docEl.clientWidth;        if (!clientWidth) return;        docEl.style.fontSize = 10*(clientWidth / 320) + ‘px‘;      };   recalc();  //判斷是否支援監聽事件 ,不支援則停止  if (!doc.addEventListener) return;  //註冊翻轉事件  win.addEventListener(resizeEvt, recalc, false); })(document, window);

方法二:按照iPhone6的尺寸設計    是375/25=15px

(function (docs, win) {  var docEls = docs.documentElement,  resizeEvts = ‘orientationchange‘ in window ? ‘orientationchange‘ : ‘resize‘,  recalcs = function () {  //getBoundingClientRect()這個方法返回一個矩形對象  window.rem = docEls.getBoundingClientRect().width/25;  docEls.style.fontSize = window.rem + ‘px‘;};  recalcs();  if (!docs.addEventListener) return;  win.addEventListener(resizeEvts, recalcs, false);})(document, window);

方式三:採用media

1 html { 2     font - size: 20 px; 3 } 4 @media only screen and(min - width: 401 px) { 5     html { 6         font - size: 25 px!important; 7     } 8 } 9 @media only screen and(min - width: 428 px) {10     html {11         font - size: 26.75 px!important;12     }13 }14 @media only screen and(min - width: 481 px) {15     html {16         font - size: 30 px!important;17     }18 }19 @media only screen and(min - width: 569 px) {20     html {21         font - size: 35 px!important;22     }23 }24 @media only screen and(min - width: 641 px) {25     html {26         font - size: 40 px!important;27     }28 }複製代碼

推薦使用的 js方式設定

移動端之js控制rem,適配字型

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.