移動端佈局(less+rem)

html

  在html使用了網上阿里的函數來計算根元素的字體(固然能夠寫在其餘地方,只要生效就能夠)css

<!doctype html>
<html lang="en">
<head>
  <meta charset="utf-8">
  <meta name="apple-mobile-web-app-capable" content="yes">
  <meta name="viewport" content="initial-scale=1, maximum-scale=1, user-scalable=no, width=device-width" />
  <title>xxx</title>
  <base href="./">
  <link rel="icon" type="image/x-icon" href="favicon.ico">
  <style type="text/css">
    .icon {
      width: 1em; height: 1em;
      vertical-align: -0.15em;
      fill: currentColor;
      overflow: hidden;
    }
  </style>
</head>
<body>
  <app-root></app-root>

  <!--調試插件-->
  <!--<script src="http://mtestzhanqi.artqiyi.com/views/common/libs/vconsole.min.js "></script>-->
  <script>
    /*
     * 根據屏幕寬度改變根元素字體
     */
    (function(doc, win) {
      var docEl = doc.documentElement,
        resizeEvt = 'orientationchange' in window ? 'orientationchange' : 'resize',
        recalc = function() {
          var clientWidth = docEl.clientWidth;
          if (!clientWidth) return;
          docEl.style.fontSize = window.screen.width / 10 + 'px';
        };
      if (!doc.addEventListener) return;
      win.addEventListener(resizeEvt, recalc, false);
      doc.addEventListener('DOMContentLoaded', recalc, false);
    })(document, window);
  </script>
</body>
</html>

  這裏主要使用了函數計算根元素字體,將屏幕分紅了10分,在iphone6下面根元素字體就是37.5px;html

less函數

  這是使用了less的unit函數來添加單位,將全部的須要轉化的css都寫到一個公共的less文件裏面,經過@import導入進去。(這裏只是粘貼了一部分)vue

.fs(@px) {
  font-size: unit(@px / 37.5, rem);
}

/*----- 寬度 -----*/
.w(@px) {
  width: unit(@px / 37.5, rem);
}

/*----- 高度 -----*/
.h(@px) {
  height: unit(@px / 37.5, rem);
}

/*----- 行高 -----*/
.lh(@px) {
  line-height: unit(@px / 37.5, rem);
}

/*----- 背景尺寸 -----*/
.b_s(@px, @px) {
  -webkit-background-size: unit(@px / 37.5, rem) unit(@px / 37.5, rem);
  background-size: unit(@px / 37.5, rem) unit(@px / 37.5, rem);
}
/**
* [背景尺寸,設置寬度,高度auto]
 */
.b_s1(@px) {
  -webkit-background-size: unit(@px / 37.5, rem) auto;
  background-size: unit(@px / 37.5, rem) auto;
}

.b_s2(@px) {
  -webkit-background-size: auto unit(@px / 37.5, rem);
  background-size: auto unit(@px / 37.5, rem);
}

/*----- margin -----*/
.mt(@px) {
  margin-top: unit(@px / 37.5, rem);
}
.mr(@px) {
  margin-right: unit(@px / 37.5, rem);
}
.mb(@px) {
  margin-bottom: unit(@px / 37.5, rem);
}
.ml(@px) {
  margin-left: unit(@px / 37.5, rem);
}

  將iphone6的屏幕分紅10分,因此就除了37.5,用unit函數來添加單位(這裏是相對iphone6的屏幕的,相信大部分公司移動端的設計圖都是根據iphone6設計的)。web

在其餘less文件裏面使用

@import "../less/bass.less";
.sort-title {
  .fs(17);
  color: @color0;
  font-family: PingFang-SC-Medium;
  .mt(20);
  .mb(15);
}

   .fs, .mt這些就是less的函數,最終編譯好的文件就是rem了。(總之,在iphon6或者iphone7,屏幕是376px,不管你上面怎麼分配比例,在chrome裏面審查你編譯後的css,鼠標放到元素上會顯示大小,這時和你設計圖上的大小相等就能夠。)chrome

總結

  本人感受這樣仍是比較方便的,不用像那樣設置根元素100px,而後根據sublime的插件來計算,或者更復雜的計算等等。咱們項目使用angular4,angular4能夠自動編譯less,只需向上面那樣直接調用函數就能夠,並且能夠作一些運算,仍是比較方便的,固然vue裏面你只要安裝less插件,就能夠讓vue-cli來編譯了,若是這些你都沒用,你還能夠經過Koala來編譯成css,經過link來引入。vue-cli

相關文章
相關標籤/搜索