欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Vue前端項(xiàng)目自適應(yīng)布局的簡(jiǎn)單方法

 更新時(shí)間:2022年06月08日 16:56:11   作者:長(zhǎng)不大的大灰狼  
最近項(xiàng)目開發(fā)中遇到一個(gè)需求,需要實(shí)現(xiàn)寬度自動(dòng)適應(yīng),所以下面這篇文章主要給大家介紹了關(guān)于Vue前端項(xiàng)目自適應(yīng)布局的相關(guān)資料,文中通過實(shí)例代碼介紹的非常詳細(xì),需要的朋友可以參考下

一、單位尺寸

  • px
  • %
  • vw、vh: 窗口
  • em: 針對(duì)父元素的font-size
  • rem:“root em”的縮寫,是一個(gè)相對(duì)長(zhǎng)度單位;rem單位作用于非根元素時(shí),相對(duì)于根元素(html)字體大小,rem單位作用于根元素字體大小時(shí),相對(duì)于其出初始字體大小。

說明:

谷歌瀏覽器,字體的默認(rèn)大小是16px;

如果父元素item1的font-size=50px,那么子元素item2的1em=50px。

如果html的頂層font-size為20px; 那么子元素item2的1rem就為20px。

二、基于rem實(shí)現(xiàn)自適應(yīng)布局

假設(shè)頁面的根元素,font-size的大小沒有設(shè)置,默認(rèn)為16px。

經(jīng)過設(shè)計(jì),我們的頁面主體部分寬為500px; 高為200px。要想轉(zhuǎn)換為自適應(yīng)布局,我們只需要將px轉(zhuǎn)換為rem就可以。

轉(zhuǎn)換后:

寬為:500/16rem

高為:200/16rem

附:html5頁面 的rem 布局適配方法

rem 布局適配方案

主要方法為:

  • 按照設(shè)計(jì)稿與設(shè)備寬度的比例,動(dòng)態(tài)計(jì)算并設(shè)置 html 根標(biāo)簽的 font-size 大?。?/li>
  • css 中,設(shè)計(jì)稿元素的寬、高、相對(duì)位置等取值,按照同等比例換算為 rem 為單位的值;
  • 設(shè)計(jì)稿中的字體使用 px 為單位,通過媒體查詢稍作調(diào)整。

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

精簡(jiǎn)通用版本:

!(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);
    }

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

//對(duì)H5活動(dòng)推過頁面,寬高比例有所要求,可適當(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媒體查詢?cè)O(shè)置 rem

簡(jiǎn)單易用 缺乏靈活度 請(qǐng)看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寫法并不唯一固定。

總結(jié)

到此這篇關(guān)于Vue前端項(xiàng)目自適應(yīng)布局的文章就介紹到這了,更多相關(guān)Vue前端自適應(yīng)布局內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

最新評(píng)論