移动端html font-size动态计算设置

设计稿以750px为宽度,其中有个元素是100px,如何设置font-size,使同一段代码在不同的设备上保持一致?

设计稿宽度设备宽度html font-size元素宽度css代码
750px320px(iphone5)?100px1rem
750px375px(iphone6/7\iphoneX)50px100px1rem
750px414px(iphone6P/7P?100px1rem

通过上面的表格可以得出,只要每个设备计算出font-size几个适配。
375/50=7.5 => 414/?=7.5 => 每个设备动态计算设备宽度/7.5即可。

(function () {
            function resizeBaseFontSize() {
                var rootHtml = document.documentElement,
                    deviceWidth = rootHtml.clientWidth;
                if (deviceWidth > 640) {
                    deviceWidth = 640;
                }
                rootHtml.style.fontSize = deviceWidth / 7.5 + "px";
            }
            resizeBaseFontSize();
            window.addEventListener("resize", resizeBaseFontSize, false);
            window.addEventListener("orientationchange", resizeBaseFontSize, false);
        })();

示例:

//网页html部分,js部分同上
<style>
        * {
            padding: 0px;
            margin: 0px;
        }
    </style>
    <div style="width:1rem;height:1rem;background: black;font-size:0.3rem;color:aliceblue;">宽:1rem</div>
    <div style="width:2rem;height:1rem;background: orange;font-size:0.3rem;color:aliceblue;">宽:2rem</div>
    <div style="width:4rem;height:1rem;background: green;font-size:0.3rem;color:aliceblue;">宽:4rem</div>
    <div style="width:6rem;height:1rem;background: yellowgreen;font-size:0.3rem;color:aliceblue;">宽:6rem</div>
    <div style="width:7.5rem;height:1rem;background: red;font-size:0.3rem;color:aliceblue;">宽:7.5rem</div>
    <div style="width:100%;">
            范德萨范德萨范德萨范德萨范德萨范德萨范德萨范德萨范德萨
    </div>

iphone5/SE


image.png

iphone6/7


iphone6P


可以看到我们的页面font-size是自动变化的(根据对应的设备宽度),并且页面的比例保持不变。以后我们只要保持设计稿是750px宽度,设计稿里面的元素,如果是100px,那么在css中就写1rem,以此类推,如果是12px,那么就是0.12rem。就是:设计稿元素尺寸/100

以下为设计稿要求:

  1. 设计稿宽度:750px
  2. css全部采用rem
  3. html的font-size动态计算:设备宽度/7.5
  4. 元素尺寸计算为:设计稿尺寸(px)/100 ,(注意单位为rem)

更新最新的vw布局方式,淘宝前端布局已经不维护了

在前端开发中实战

//vue示例
<style scoped lang="stylus">
//定义一个方法用来从px转vm
  p2v(p)
    p / 750 * 100vw;//假设设计稿是750宽度
  $orange = #ff7000
  .banks
    padding p2v(12)//此处计算得是1.6vw,把页面拉伸到750px宽度下,box显示12px
    border 1px olid $orange
    box-shadow 1vw 1vw 1vw $orange
    li
      color $orange
      width p2v(375)//此处计算得是50vw,把页面拉伸到750px宽度下,box显示375px


对下面的代码加注释:<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta http-equiv="X-UA-Compatible" content="IE=edge"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>Document</title> </head> <style> #backg { width: 919px; height: 272px; } div img { width: 300px; height: 222px; margin-top: 20px; margin-left: 10px; } #h3 { color: rgb(132, 106, 90); font-weight: 600; font-size: 23px; margin-top: -224px; margin-left: 340px; } #p1 { font-size: 14px; color: rgb(20, 111, 158); font-weight: 550; margin-top: 13px; margin-left: 340px; } #p2 { width: 575px; font-size: 13.2px; color: black; text-indent: 2em; margin-top: -3px; margin-left: 340px; } #p2 span { color: rgb(20, 111, 158); font-style: italic; text-decoration: underline; } #p3 { font-size: 14px; color: rgb(20, 111, 158); font-weight: 550; margin-top: 10px; margin-left: 340px; } #p3 span { color: red; font-size: 20px; font-weight: 800; } hr { width: 560px; margin-top: -2px; margin-left: 340px; } #p4 { color: rgb(132, 106, 90); font-weight: 540; font-size: 23px; margin-top: 12px; margin-left: 340px; } #p4 span { color: red; font-weight: 800; } </style> <body> <div id="backg"> <img src="./img/bk01.png" alt=""> <div id="h3">Web前端开发工程师</div> <p id="p1">技术要求:</p> <p id="p2">对常用的一些Js框架了解,如jQuery、YUuI等。掌握最基本的JavaScript计算方法编写。对目前互联网流行的网页制作方法(Web2.0) HTML+CSS,以及各大浏览器兼容性有很大的了解。对前沿技术(HTML5+CSS3)的基本掌握。<span>Web前端技术</span>你究竟掌握了多少...</p> <p id="p3">更新时间:2015年05月19日20点(已有<span>325</span>人点赞)</p> <hr> <p id="p4">相关技术文章 <span>8</span> 篇</p> </div> </body> </html>
05-24
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值