移動端布Rem

來源:互聯網
上載者:User

標籤:物理   str   app   type   資源   function   element   for   http   

一、最好用沒有之一

http://www.jianshu.com/p/b00cd3506782

雖然博主說這個方案已經到期了 但是新方案還沒有理解 就接著沿用這個

可以根據自己常用的設計稿的寬度修改 640 為 750 

<!DOCTYPE html><html lang="en"><head>  <meta charset="UTF-8">   <meta name="viewport" content="width=device-width,minimum-scale=1.0,maximum-scale=1.0,user-scalable=no">  <script>   (function (doc, win) {        var docEl = doc.documentElement,            resizeEvt = ‘orientationchange‘ in window ? ‘orientationchange‘ : ‘resize‘,            recalc = function () {                var clientWidth = docEl.clientWidth;                if (!clientWidth) return;                if(clientWidth>=640){                    docEl.style.fontSize = ‘100px‘;                }else{                    docEl.style.fontSize = 100 * (clientWidth / 640) + ‘px‘;                }            };        if (!doc.addEventListener) return;        win.addEventListener(resizeEvt, recalc, false);        doc.addEventListener(‘DOMContentLoaded‘, recalc, false);    })(document, window);</script>  /*你引進的資源*/<title>標題</title> </head><body> /*你的代碼*/</body></html>_minooo_連結:http://www.jianshu.com/p/b00cd3506782來源:簡書著作權歸作者所有。商業轉載請聯絡作者獲得授權,非商業轉載請註明出處。
二、高清方案

高清方案就是根據裝置螢幕的DPR(裝置像素比,又稱DPPX,比如dpr=2時,表示1個CSS像素由4個物理像素點組成)** 動態設定 html 的font-size, 同時根據裝置DPR調整頁面的縮放值,進而達到高清效果**。

此方案也是預設 1rem = 100px,所以你布局的時候,完全可以按照設計師給你的寫各種尺寸啦。

注意:

  • rem適合寫固定尺寸。其餘的根據需要換成flex或者百分比。
  • 在X5核心的瀏覽器下,字型會自動放大,解決方案: 

 

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

 

  • 如果元素的寬度超過寬度的一半(寬為640或750),果斷使用百分比寬度,或者flex布局。

    否則可能會有橫向捲軸。

‘use strict‘;/** * @param {Boolean} [normal = false] - 預設開啟頁面壓縮以使頁面高清;   * @param {Number} [baseFontSize = 100] - 基礎fontSize, 預設100px; * @param {Number} [fontscale = 1] - 有的業務希望能放大一定比例的字型; */const win = window;export default win.flex = (normal, 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 = normal ? 1 : 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 = normal ? ‘50px‘ : `${_baseFontSize / 2 * dpr * _fontscale}px`;};_minooo_連結:http://www.jianshu.com/p/985d26b40199來源:簡書著作權歸作者所有。商業轉載請聯絡作者獲得授權,非商業轉載請註明出處。

 

移動端布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.