Home > Web Front-end > H5 Tutorial > body text

Detailed explanation of HTML5 page rem layout adaptation method

小云云
Release: 2018-01-15 09:54:19
Original
5215 people have browsed it

This article mainly introduces the detailed explanation of the HTML5 page rem layout adaptation method. The editor thinks it is quite good. Now I will share it with you and give you a reference. Let’s follow the editor to take a look, I hope it can help everyone.

rem layout adaptation scheme

The main method is:

  1. Dynamic calculation based on the ratio of the design draft to the width of the device And set the font-size size of the html root tag; in

  2. #css, the width, height, relative position, etc. of the design draft elements are converted into values ​​in rem units according to the same proportion. ;

  3. The font in the design draft uses px as the unit and is slightly adjusted through media queries.

1 Dynamically set html tag font-size size

Simplified universal version:


##

!(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) + &#39;px&#39; ;
  }

  var evt = &#39;onorientationchange&#39; in win ? &#39;orientationchange&#39; : &#39;resize&#39;;

  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));
Copy after login

Highly accurate version:


(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 + &#39;px&#39;;

        // 误差、兼容性处理
        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 + &#39;px&#39;;
        }
    }

    var timer;

    function dbcRefresh() {
        clearTimeout(timer);
        timer = setTimeout(setFontSize, 100);
    }

    //窗口更新动态改变 font-size
    WIN.addEventListener(&#39;resize&#39;, dbcRefresh, false);
    //页面显示时计算一次
    WIN.addEventListener(&#39;pageshow&#39;, function(e) {
        if (e.persisted) { 
            dbcRefresh() 
        }
    }, false);
    setFontSize();
})(window)


//对H5活动推过页面,宽高比例有所要求,可适当调整

function adjustWarp(warpId = &#39;#warp&#39;) {

    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: &#39;relative&#39;,
        top: 0,
        left: &#39;auto&#39;,
        margin: &#39;0 auto&#39;
    });

    // 重新计算 rem
    window.setFontSize(width);
}
Copy after login

2 Set rem through CSS3 media query

Easy to use, lack of flexibility, please see demo you know


@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}}
Copy after login

can be modified appropriately according to personal project needs and product design. The above demo writing method is not unique and fixed.

Related recommendations:


javascript uses rem to do responsive development example sharing

Two mobile terminal rem layout implementations Method

Example to explain js implementation of rem automatic matching calculation font-size

The above is the detailed content of Detailed explanation of HTML5 page rem layout adaptation method. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template