程序员技术干货JavaScript 进阶营

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

2017-08-17  本文已影响322人  rayman_v

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>
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>
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>

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>
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>

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>
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>
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>


总结

  1. <meta name="viewport" content="width=360"> 能应付大部分 h5 活动页面布局,但不被广泛使用;
  2. 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;}
  .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>
上一篇 下一篇

猜你喜欢

热点阅读