美文网首页程序员技术干货JavaScript 进阶营
html5 的 rem、px、viewport 在页面布局中的作

html5 的 rem、px、viewport 在页面布局中的作

作者: rayman_v | 来源:发表于2017-08-17 18:43 被阅读322次

viewport

1. 没有 viewport
<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <style>
  *{margin: 0; padding: 0;}
  #full{background-color: red; width: 100%; height: 100px; font-size: 40px;}
  </style>
</head>
<body>
  <div id="full"></div>
  <script>document.getElementById('full').textContent = document.documentElement.clientWidth;</script>
</body>
</html>
  • 没有设置 viewport 的情况下,不同尺寸下的手机屏幕默认宽度都是980宽;
2. 设置 viewport 为固定的 width
<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=360">
  <style>
  *{margin: 0; padding: 0;}
  #full{background-color: red; width: 100%; height: 100px; font-size: 40px;}
  </style>
</head>
<body>
  <div id="full"></div>
  <script>document.getElementById('full').textContent = document.documentElement.clientWidth;</script>
</body>
</html>
  • 设置 <meta name="viewport" content="width=360"> 后,会使页面在不同设备上显示相同宽度,事例中不管 iPhone 5iPhone 6 页面都会变成 360px 宽;
3. 设置 viewport 为设备的 width
<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width">
  <style>
  *{margin: 0; padding: 0;}
  #full{background-color: red; width: 100%; height: 100px; font-size: 40px;}
  </style>
</head>
<body>
  <div id="full"></div>
  <script>document.getElementById('full').textContent = document.documentElement.clientWidth;</script>
</body>
</html>
  • 设置 <meta name="viewport" content="width=device-width"> 后,页面宽度会识别为硬件设备最优的宽度显示,这是使用最广泛的设置方式。

px 布局

1. viewport 为固定宽度下的 px 布局
<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=360">
  <style>
  *{margin: 0; padding: 0;}
  #full{background-color: red; height: 100px; font-size: 0;}
  .item{background-color: yellow; height: 100px; width: 120px; display: inline-block; margin: 0 3px 0 0; font-size: 40px;}
  </style>
</head>
<body>
  <div id="full">
    <div class="item">1</div>
    <div class="item">2</div>
    <div class="item">3</div>
  </div>
</body>
</html>
  • 因为指定了宽度 360 ,页面在任何设备上都可以横向布满3个 .item ;
2. viewport 为设备宽度下的 px 布局
<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width">
  <style>
  *{margin: 0; padding: 0;}
  #full{background-color: red; height: 100px; font-size: 0;}
  .item{background-color: yellow; height: 100px; width: 120px; display: inline-block; margin: 0 3px 0 0; font-size: 40px;}
  </style>
</head>
<body>
  <div id="full">
    <div class="item">1</div>
    <div class="item">2</div>
    <div class="item">3</div>
  </div>
</body>
</html>
  • 由于 <meta name="viewport" content="width=device-width"> 页面在不同设备的宽度不同,导致在 iPhone 5 下为 320px,导致换行,而在 iPhone 6 下为 375px,横放3个框有余;

rem 布局

1. device-width 下的 rem
<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width">
  <style>
  *{margin: 0; padding: 0;}
  #full{background-color: red; height: 100px; font-size: 0;}
  .item{background-color: yellow; height: 100px; width: 1rem; display: inline-block; margin: 0 3px 0 0; font-size: 14px;}
  </style>
</head>
<body>
  <div id="full">
    <div class="item">1</div>
    <div class="item">2</div>
    <div class="item">3</div>
  </div>
</body>
</html>
  • rem 是以 <html> 元素的 font-size 为基数转化成的 px 值,例如 <html> 标签默认值 font-size: 16px1rem 即等于 16px
2. 手动设置 rem 基数
<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width">
  <style>
  *{margin: 0; padding: 0;}
  @media (max-width: 375px) {
    html {font-size: 100px;}
  }
  @media (max-width: 320px) {
    html {font-size: 50px;}
  }
  #full{background-color: red; height: 100px; font-size: 0;}
  .item{background-color: yellow; height: 100px; width: 1rem; display: inline-block; margin: 0 3px 0 0; font-size: 14px;}
  </style>
</head>
<body>
  <div id="full">
    <div class="item">1</div>
    <div class="item">2</div>
    <div class="item">3</div>
  </div>
</body>
</html>
  • 通过媒体查询,我们可以分别为 iPhone 5 和 iPhone 6 设置不同的 rem 基数,实现不同设备宽度下,布局不同;
3. 动态设置 rem 基数
<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width">
  <script>
  (function(doc,win,wid){
    var rootEle = doc.documentElement,
        wid = wid || 750;
    recalc();
    function recalc(){
      rootEle.style.fontSize = 100*(rootEle.clientWidth/wid)+"px"
    }
    win.addEventListener('orientationchange',recalc,false);
    win.addEventListener('resize',recalc,false);
  })(document,window,750);</script>
  <style>
  *{margin: 0; padding: 0;}
  #full{background-color: red; font-size: 0;}
  .item{background-color: yellow; height: 2rem; width: 2rem; display: inline-block; margin: 0 3px 0 0; font-size: 14px;}
  </style>
</head>
<body>
  <div id="full">
    <div class="item">1</div>
    <div class="item">2</div>
    <div class="item">3</div>
  </div>
</body>
</html>

  • 当使用 js 动态计算 <html>font-size 再加上使用 rem 布局,最终结果就跟 <meta name="viewport" content="width=360"> 加上 px 布局的效果是一样的,就是不同设备宽度能够显示的页面宽度一样;
  • 这段 js 代码的意思是,传入的 750 为设计图总宽度,在任何屏幕宽度内,1rem 等于设计图中的 100px
  • 举例:设计图总宽度为 640px 的情况下,应该在调用该 js 时,把 750 替换成 640,当设计图中有一个 44px * 44px 的按钮,css 应为 width: 0.44rem; height: 0.44rem;

总结

  1. <meta name="viewport" content="width=360"> 能应付大部分 h5 活动页面布局,但不被广泛使用;
  2. rem 虽然原理有点繁琐,但被广泛推广使用,这是有原因的,假设一种开发需求:
  • 上面的 banner 需要在不同手机上,等比缩放展示同样的内容,而下面的 text 则需要越大的屏幕,一行显示的文字越多,这种情况下,就需要 pxrem 能同时使用在同一页面内。
<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width">
  <script>
  (function(doc,win,wid){
    var rootEle = doc.documentElement,
        wid = wid || 750;
    recalc();
    function recalc(){
      rootEle.style.fontSize = 100*(rootEle.clientWidth/wid)+"px"
    }
    win.addEventListener('orientationchange',recalc,false);
    win.addEventListener('resize',recalc,false);
  })(document,window,750);</script>
  <style>
  *{margin: 0; padding: 0;}
  .banner{background-color: red; width: 7.5rem; height: 4rem;}
  .text{background-color: yellow; font-size: 16px;}
  </style>
</head>
<body>
  <div class="banner"></div>
  <div class="text">“我们一定不负重托,不辱使命。”这是习近平总书记上任伊始作出的庄严承诺。五年过去,中国发展站到新的历史起点上,中国特色社会主义进入新的发展阶段,实现了从站起来、富起来到强起来的历史性飞跃,世界越来越看好中国。</div>
</body>
</html>

相关文章

  • html5 的 rem、px、viewport 在页面布局中的作

    viewport 1. 没有 viewport 没有设置 viewport 的情况下,不同尺寸下的手机屏幕默认宽度...

  • 2019-08-29

    px、rem、em的区别 随着css学习的不断深入页面也随之丰富,那么em、rem、px是我们在页面布局中经常会用...

  • 手机页面布局杂谈

    手机页面布局方案有好多种,但不管你怎样做,都需要用viewport, viewport就不在这谈了。 rem布局,...

  • 项目中常用到的js方法整理

    一.change_view.js 用于手机端页面rem布局换算 640px=>6.4rem;100px=>1rem...

  • 移动端、PC端屏幕适配

    移动端适配 页面引入ydui.flexible.js页面布局采用rem布局rem计算方式:设计图尺寸px / 10...

  • em、rem、px区别

    css中如何区分em、rem、px? 随着css学习的不断深入页面也随之丰富,那么em、rem、px是我们在页面布...

  • rem适配

    chrome浏览器字体默认16px 最小12px em rem rem适配 viewport 1物理像素的实现

  • 手机端rem实现自适应布局

    rem是什么? 理解rem 实战布局 需求:假如UI设计的手机页面宽度是750px; 需求制作页面时还原度高,兼容...

  • 移动端H5 UI适配

    一、不允许页面缩放 二、设置script 布局采用rem,计算方式切图的px/100=xxx.rem三、横屏状态检测

  • 响应式布局

    布局类别 1.固定网页布局:设置固定宽度,px为单位。常见PC端页面。 2.流式布局+伸缩布局+rem+媒体查询:...

网友评论

    本文标题:html5 的 rem、px、viewport 在页面布局中的作

    本文链接:https://www.haomeiwen.com/subject/rsjgrxtx.html