国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 開發 > HTML5 > 正文

詳解html5頁面 rem 布局適配方法

2024-09-05 07:22:19
字體:
來源:轉載
供稿:網友

rem 布局適配方案

主要方法為:

  1. 按照設計稿與設備寬度的比例,動態計算并設置 html 根標簽的 font-size 大小;
  2. css 中,設計稿元素的寬、高、相對位置等取值,按照同等比例換算為 rem 為單位的值;
  3. 設計稿中的字體使用 px 為單位,通過媒體查詢稍作調整。

1 動態設置 html 標簽 font-size 大小

精簡通用版本:

!(function(win, doc){  function setFontSize() {    // 獲取window 寬度    // zepto實現 $(window).width()就是這么干的    var winWidth =  window.innerWidth;    // doc.documentElement.style.fontSize = (winWidth / 640) * 100 + 'px' ;        // 640寬度以上進行限制 需要css進行配合    var size = (winWidth / 640) * 100;    doc.documentElement.style.fontSize = (size < 100 ? size : 100) + 'px' ;  }  var evt = 'onorientationchange' in win ? 'orientationchange' : 'resize';  var timer = null;  win.addEventListener(evt, function () {    clearTimeout(timer);    timer = setTimeout(setFontSize, 300);  }, false);  win.addEventListener("pageshow", function(e) {    if (e.persisted) {      clearTimeout(timer);      timer = setTimeout(setFontSize, 300);    }  }, false);  // 初始化  setFontSize();}(window, document));

高配精確版本:

(function(WIN) {    var  setFontSize = WIN.setFontSize = function (_width) {        var  docEl = document.documentElement;         // 獲取當前窗口的寬度        var  width = _width || docEl.clientWidth; // docEl.getBoundingClientRect().width;        // 大于 1080px 按 1080        if (width > 1080) {             width = 1080;        }        var  rem = width / 10;        console.log(rem);        docEl.style.fontSize = rem + 'px';        // 誤差、兼容性處理        var  actualSize = win.getComputedStyle && parseFloat(win.getComputedStyle(docEl)["font-size"]);        if (actualSize !== rem && actualSize > 0 && Math.abs(actualSize - rem) > 1) {            var remScaled = rem * rem / actualSize;            docEl.style.fontSize = remScaled + 'px';        }    }    var timer;    function dbcRefresh() {        clearTimeout(timer);        timer = setTimeout(setFontSize, 100);    }    //窗口更新動態改變 font-size    WIN.addEventListener('resize', dbcRefresh, false);    //頁面顯示時計算一次    WIN.addEventListener('pageshow', function(e) {        if (e.persisted) {             dbcRefresh()         }    }, false);    setFontSize();})(window)//對H5活動推過頁面,寬高比例有所要求,可適當調整function adjustWarp(warpId = '#warp') {    const $win = $(window);    const height = $win.height();    let width = $win.width();    // 考慮導航欄情況    if (width / height < 360 / 600) {        return;    }    width = Math.ceil(height * 360 / 640);    $(warpId).css({        height,        width,        postion: 'relative',        top: 0,        left: 'auto',        margin: '0 auto'    });    // 重新計算 rem    window.setFontSize(width);}

2 通過 CSS3媒體查詢設置 rem

簡單易用 缺乏靈活度 請看demo 你懂的

@media screen and ( min-width: 320px){html{font-size:50px}}@media screen and ( min-width: 360px){html{font-size:56.25px}}@media screen and ( min-width: 375px){html{font-size:58.59375px}}@media screen and ( min-width: 384px){html{font-size:60px}}@media screen and ( min-width: 400px){html{font-size:62.5px}}@media screen and ( min-width: 414px){html{font-size:64.6875px}}@media screen and ( min-width: 424px){html{font-size:66.25px}}@media screen and ( min-width: 480px){html{font-size:75px}}@media screen and ( min-width: 540px){html{font-size:84.375px}}@media screen and ( min-width: 640px){html{font-size:100px}}

根據個人項目需求和產品設計可適當修改,以上demo寫法并不唯一固定。

以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持武林網。

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 百色市| 双城市| 石阡县| 平遥县| 壶关县| 左云县| 浏阳市| 耒阳市| 理塘县| 孟村| 冷水江市| 鄂州市| 和政县| 三江| 淳化县| 务川| 城固县| 博客| 临沧市| 乌兰县| 瓦房店市| 苍南县| 安康市| 乌拉特前旗| 江山市| 民权县| 蓝山县| 南和县| 汶上县| 大冶市| 津市市| 聊城市| 图们市| 静安区| 阿荣旗| 南江县| 来安县| 上犹县| 雅安市| 正安县| 连州市|