溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點(diǎn)擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

html5頁面rem布局適配方法有哪些

發(fā)布時(shí)間:2020-10-24 16:03:12 來源:億速云 閱讀:151 作者:小新 欄目:web開發(fā)

這篇文章主要介紹html5頁面rem布局適配方法有哪些,文中介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們一定要看完!

rem 布局適配方案

主要方法為:

  1. 按照設(shè)計(jì)稿與設(shè)備寬度的比例,動態(tài)計(jì)算并設(shè)置 html 根標(biāo)簽的 font-size 大??;

  2. css 中,設(shè)計(jì)稿元素的寬、高、相對位置等取值,按照同等比例換算為 rem 為單位的值;

  3. 設(shè)計(jì)稿中的字體使用 px 為單位,通過媒體查詢稍作調(diào)整。

1 動態(tài)設(shè)置 html 標(biāo)簽 font-size 大小

精簡通用版本:

!(function(win, doc){
  function setFontSize() {
    // 獲取window 寬度
    // zepto實(shí)現(xiàn) $(window).width()就是這么干的
    var winWidth =  window.innerWidth;
    // doc.documentElement.style.fontSize = (winWidth / 640) * 100 + 'px' ;
    
    // 640寬度以上進(jìn)行限制 需要css進(jìn)行配合
    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; 
        // 獲取當(dāng)前窗口的寬度
        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);
    }

    //窗口更新動態(tài)改變 font-size
    WIN.addEventListener('resize', dbcRefresh, false);
    //頁面顯示時(shí)計(jì)算一次
    WIN.addEventListener('pageshow', function(e) {
        if (e.persisted) { 
            dbcRefresh() 
        }
    }, false);
    setFontSize();
})(window)


//對H5活動推過頁面,寬高比例有所要求,可適當(dāng)調(diào)整

function adjustWarp(warpId = '#warp') {

    const $win = $(window);
    const height = $win.height();
    let width = $win.width();

    // 考慮導(dǎo)航欄情況
    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'
    });

    // 重新計(jì)算 rem
    window.setFontSize(width);
}

2 通過 CSS3媒體查詢設(shè)置 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}}

根據(jù)個(gè)人項(xiàng)目需求和產(chǎn)品設(shè)計(jì)可適當(dāng)修改,以上demo寫法并不唯一固定。

以上是html5頁面rem布局適配方法有哪些的所有內(nèi)容,感謝各位的閱讀!希望分享的內(nèi)容對大家有幫助,更多相關(guān)知識,歡迎關(guān)注億速云行業(yè)資訊頻道!

向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI