欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

详解H5 活动页之移动端 REM 布局适配方法

程序员文章站 2022-06-30 11:13:34
这篇文章主要介绍了详解H5 活动页之移动端 REM 布局适配方法的相关资料,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧... 17-12-07...

拿到设计稿后,如何进行布局还原?

如果只需要做非精确的响应式设计,那么使用媒体查询来实现就 ok 了。如果需要精确还原设计稿,则一般通过缩放来实现。常见方案有基于 viewport 和基于 rem 的缩放方案。

1 viewport 缩放方案

在移动端,可以通过 viewport 缩放页面大小比率达到目的。

简单来说,即所有宽高像素与视觉稿输出相同,然后通过页面宽度与视觉稿的宽度比率,动态设置 viewport。缩放方案核心代码参考:

(function () {
    var docel = document.documentelement;
    var ismobile = window.ismobile /android|webos|iphone|ipad|ipod|blackberry|iemobile|opera mini|mobi/i.test(navigator.useragent);

    function setscale() {
        var pagescale = 1;

        if (window.top !== window) {
            return pagescale;
        }

        var width = docel.clientwidth || 360;
        var height = docel.clientheight || 640;
        if (width / height >= 360 / 640) {
            // 高度优先
            pagescale = height / 640;
        } else {
            pagescale = width / 360;
        }

        var content = 'width=' + 360 + ', initial-scale=' + pagescale 
          + ', maximum-scale=' + pagescale + ', user-scalable=no';
        document.getelementbyid('viewport').setattribute('content', content);
        window.pagescale = pagescale;
    }
    if (ismobile) {
        setscale();
    } else {
        docel.classname += ' pc';
    }
})()

这种方案在我们去年的一次 h5 活动页设计中进行了相关实践。

但是如果希望 pc 上也能显示,由于没有 viewport 的缩放概念,只能以固定值来设定,这个效果就不太理想。

2 rem 布局适配方案

rem 布局适配方案被提到的比较多,在各大互联网企业产品中都有较为广泛的应用。

简单来说其方法为:

  1. 按照设计稿与设备宽度的比例,动态计算并设置 html 根标签的 font-size 大小;
  2. css 中,设计稿元素的宽、高、相对位置等取值,按照同等比例换算为 rem 为单位的值;
  3. 设计稿中的字体使用 px 为单位,通过媒体查询稍作调整。

下面我们举个例子来说明。

2.1 动态设置 html 标签 font-size 大小

第一个问题是 html 标签的 font-size 动态计算。这取决于如何约定换算比例,以页面宽度十等份为例,核心代码参考:

(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') {
    // if (window.ismobile) return;
    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.2 元素大小取值方法

第二个问题是元素大小的取值。

以设计稿宽度 1080px 为例,我们将宽度分为 10 等份以便于换算,那么 1rem = 1080 / 10 = 108px 。其换算方法为:

const px2rem = function(px, rem = 108) {
    let remval = parsefloat(px) / rem;
    if (typeof px === "string" && px.match(/px$/)) { 
        remval += 'rem';
    }

    return remval;
}

例如,设计稿中有一个图片大小为 460x210 ,相对页面位置 top: 321px; left: 70; 。按照如上换算方式,得到该元素最终的 css 样式应为:

.img_demo {
    position: absolute;
    background-size: cover;
    background-image: url('demo.png');
    top: 2.97222rem;
    left: 0.64814rem;
    width: 4.25926rem;
    height: 1.94444rem;
}

2.3 rem 布局方案的开发方式

通过以上方法,rem 布局方案就得到了实现。但是手动计算 rem 的取值显然不现实。

通过 less/sass 预处理工具,我们只需要设置 mixins 方法,然后按照设计稿的实际大小来取值即可。以 less 为例,mixins 参考如下:

// px 转 rem
.px2rem(@px, @attr: 'width', @rem: 108rem) {
    @{attr}: (@px / @rem);
}

.px2remtlwh(@top, @left, @width, @height, @rem: 108rem) {
    .px2rem(@top, top, @rem);
    .px2rem(@left, left, @rem);
    .px2rem(@width, width, @rem);
    .px2rem(@height, height, @rem);
}

针对前文的示例元素,css 样式可以这样来写:

.img_demo {
    position: absolute;
    background-size: cover;
    background-image: url('demo.png');
    .px2remtlwh(321, 70, 460, 210);
}

这里,宽和高可以直接通过设计稿输出的图片元素大小读取到;top/left 的取值,可以通过在 photoshop 中移动参考线定位元素快速得到。

2.4 字体使用 px 为单位

字体使用 rem 等比缩放会出现显示上的问题,只需要针对性使用媒体查询设置几种大小即可。

示例参考:

// 字体响应式
@media screen and (max-width: 321px) {
    body {
        font-size: 13px;
    }
}

@media screen and (min-width: 321px) and (max-width: 400px) {
    body {
        font-size: 14px;
    }
}

@media screen and (min-width: 400px) {
    body {
        font-size: 16px;
    }
}

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。