前言

为了确保项目样式的可维护性和拓展性,我们一般在项目开始,跟ui设计确定一些公共设计规范,比如font-size、font-family、color等等,来统一项目的公共样式。

一、变量

  1. //
  2. // variables.less
  3. // --------------------------------------------------
  4. // font
  5. // --------------------------------------------------
  6. @font-family-default: 'Helvetica Neue', Helvetica, 'PingFang SC', 'Hiragino Sans GB', 'Microsoft Yahei', Arial, sans-serif;
  7. @font-size-title-default: 18px;
  8. @font-size-master: 16px;
  9. @font-size-main: 14px;
  10. @font-size-default: 12px;
  11. @font-size-secondary: 10px;
  12. @font-weight-bold: 600;
  13. @font-weight: 500;
  14. @font-weight-light: 400;
  15. @line-height-default: 1;
  16. // Colors
  17. // --------------------------------------------------
  18. // 通用
  19. @color-white: #FFF;
  20. @color-blue: #3969FE;
  21. @color-red: #F23D20;
  22. @color-gray: #999;
  23. @color-warn: #FA553C;
  24. // 背景色
  25. @color-bg-white: @color-white;
  26. @color-bg-gray: #F8F8F8;
  27. @color-bg-gray2: #FAFAFA;
  28. @color-bg-gray-light: #F5F5F5;
  29. @color-bg-gray-dark: #F8F9FC;
  30. @color-bg-gray-dark2: #F1F1F1;
  31. @color-bg-blue: @color-blue;
  32. @color-bg-blue-light: #F2F6FF;
  33. @color-bg-blue-dark: #4E79FF;
  34. @color-bg-red-light:rgba(250,85,60,0.05);
  35. // 边框
  36. @color-border-grey: rgba(0,0,0,0.05);
  37. // 字体颜色
  38. @color-text: #333 ; //文案色
  39. @color-text-gray-light: #666; // 浅灰色文案
  40. @color-text-gray: @color-gray; //灰色文案
  41. @color-text-white: @color-white; //有背景色的文案
  42. @color-text-blue: @color-blue;
  43. @color-text-green: #129352;
  44. @color-text-red: @color-red; // 预警的颜色
  45. @color-text-red-assistant: #FA553C;
  46. @color-text-black: #000;
  47. // 分割线
  48. @color-split-10: rgba(0,0,0,0.1);
  49. @color-split-20: rgba(0,0,0,0.2);
  50. @color-split: rgba(0,0,0,0.05); //分割线的颜色
  51. @color-split2: rgba(0,0,0,0.07); //分割线的颜色
  52. // 多弹窗层级问题 z-index
  53. @navbar-z-index: 1000;
  54. @tabbar-z-index: 1000;
  55. @backtop-z-index: 1100;
  56. @search-z-index: 1200;
  57. @product-z-index: 1200;
  58. @search-popup-z-index: @search-z-index + 10;
  59. @category-popup-z-index: @search-z-index - 10;
  60. // componets
  61. // --------------------------------------------------
  62. // chart
  63. @color-chart-blue: #5374E8;
  64. @color-chart-green: #10AC84;
  65. @color-chart-tip-bg: #243850;
  66. // popup
  67. @popup-background-color: @color-white;
  68. @popup-transition-duration: .2s;
  69. // overlay
  70. @overlay-background-color: rgba(0, 0, 0, .4);
  71. // toast
  72. @toast-font-color: @color-white;
  73. @toast-max-width: 70%;
  74. @toast-border-radius: 7px;
  75. @toast-container-padding: 24px;
  76. @toast-message-padding-top: 8.5px;
  77. @toast-background-color: rgba(0, 0, 0, .7);
  78. @toast-icon-font-size: 40px;
  79. @toast-default-width: 148px;
  80. @toast-default-min-height: 137px;
  81. @toast-default-padding: 15px;
  82. @toast-default-font-size: 14px;
  83. @toast-default-message-padding-top: 19px;
  84. @toast-text-min-width: 104px;
  85. @toast-text-font-size: 16px;
  86. @toast-text-padding: 15px 20px;
  87. @toast-position-top-distance: 50px;
  88. @toast-position-bottom-distance: 50px;

二、混合宏函数

  1. //
  2. // mixins.less
  3. // --------------------------------------------------
  4. // special font-family
  5. // --------------------------------------------------
  6. @import 'variables.less';
  7. .PingFangSC-Light() {
  8. font-family: PingFangSC-Light;
  9. }
  10. .PingFangSC-Regular() {
  11. font-family: PingFangSC-Regular;
  12. font-weight: @font-weight-light;
  13. }
  14. .PingFangSC-Medium() {
  15. font-family: PingFangSC-Medium;
  16. font-weight: @font-weight;
  17. }
  18. .PingFangSC-Semibold() {
  19. font-family: PingFangSC-Semibold;
  20. font-weight: @font-weight-bold;
  21. }
  22. .rem($px) {
  23. @return ($px / $vw_fontsize ) * 1rem;
  24. }
  25. // flex
  26. // --------------------------------------------------
  27. // 垂直居中
  28. .flex-col-center() {
  29. display: flex;
  30. flex-flow: row nowrap;
  31. align-items: center;
  32. }
  33. // 两侧
  34. .flex-between() {
  35. display: flex;
  36. flex-flow: row nowrap;
  37. align-items: center;
  38. justify-content: space-between;
  39. }

三、基础

  1. //
  2. // base.less
  3. // --------------------------------------------------
  4. html {
  5. /*滚动事件发生在 html 元素上;JS 中可以监听 html 的滚动*/
  6. overflow-y: auto;
  7. /*让 html 和浏览器窗口高度一致*/
  8. height: 100%;
  9. line-height: 1;
  10. /*少数浏览器默认背景色为浅灰色,所以设置默认背景颜色为纯白*/
  11. background-color: @color-bg-white;
  12. -webkit-font-smoothing: antialiased;
  13. -moz-osx-font-smoothing: grayscale;
  14. -webkit-text-size-adjust: 100%;
  15. }
  16. html,
  17. body {
  18. /*body 宽度大 html 度时,某些浏览器会出现内部滚动条;所以设置「html、body」宽度相同且「overflow-x: hidden」*/
  19. overflow-x: hidden;
  20. width: 100%;
  21. padding-bottom: constant(safe-area-inset-bottom);//为底下圆弧的高度 34px
  22. }
  23. body {
  24. /*设置基本字体配置*/
  25. font-size: @font-size-main;
  26. /*让绝对定位元素,根据 body 定位*/
  27. position: relative;
  28. /*设置网页基本字体颜色为浅灰色*/
  29. color: @color-text-gray-light;
  30. /*使字体渲染更顺滑*/
  31. -webkit-font-smoothing: antialiased;
  32. -moz-osx-font-smoothing: grayscale;
  33. text-rendering: optimizeLegibility;
  34. -webkit-overflow-scrolling: touch;
  35. /* body 也增加最大最小宽度限制,避免默认100%宽度的 block 元素跟随 body 而过大过小 */
  36. max-width: 540px;
  37. min-width: 320px;
  38. }
  39. #app {
  40. font-size: initial;//重置页面字体大小恢复为浏览器默认16px,
  41. }
  42. // Use box sizing on all the things!
  43. * {
  44. box-sizing: border-box;
  45. -webkit-tap-highlight-color: rgba(0, 0, 0, 0);
  46. -webkit-touch-callout: none;
  47. }
  48. body,
  49. p,
  50. h1,
  51. h2,
  52. h3,
  53. h4,
  54. h5,
  55. h6,
  56. dl,
  57. dd,
  58. ul,
  59. ol,
  60. th,
  61. td,
  62. button,
  63. figure,
  64. input,
  65. textarea,
  66. form {
  67. margin: 0;
  68. padding: 0;
  69. }
  70. /**
  71. * 不同浏览器的input、select、textarea的盒子模型宽度计算方式不同,统一为最常见的 content-box
  72. */
  73. input,
  74. select,
  75. textarea {
  76. -webkit-box-sizing: content-box;
  77. -moz-box-sizing: content-box;
  78. box-sizing: content-box;
  79. }
  80. table {
  81. /*table相邻单元格的边框间的距离设置为 0*/
  82. border-spacing: 0;
  83. /*默认情况下给tr 设置 border 没有效果,如果 table 设置了边框为合并模式:「border-collapse: collapse;」就可以了*/
  84. border-collapse: collapse;
  85. }
  86. /**
  87. * 移除浏览器部分元素的默认边框
  88. * acronym、fieldset … 等其他标签不是很常用,就不会一一列举;如果项目中用到,可以自己单独写
  89. */
  90. img,
  91. input,
  92. button,
  93. textarea {
  94. border: none;
  95. -webkit-appearance: none;
  96. }
  97. input {
  98. /*由于 input 默认不继承父元素的居中样式,所以设置:「text-align: inherit」*/
  99. text-align: inherit;
  100. }
  101. textarea {
  102. /*textarea 默认不可以放缩*/
  103. resize: none;
  104. }
  105. /**
  106. * 由于以下元素的部分属性没有继承父节点样式,所以声明这些元素的这些属性为父元素的属性
  107. * 取消这些元素 `outline` 样式
  108. */
  109. a,
  110. h1,
  111. h2,
  112. h3,
  113. h4,
  114. h5,
  115. h6,
  116. input,
  117. select,
  118. button,
  119. option,
  120. textarea,
  121. optgroup {
  122. font-family: inherit;
  123. font-size: inherit;
  124. font-weight: inherit;
  125. font-style: inherit;
  126. line-height: inherit;
  127. color: inherit;
  128. outline: none;
  129. }
  130. /**
  131. * 取消超链接元素的默认文字装饰
  132. * 另外 del、ins 标签的中划线、下划线还是挺好的,就不去掉
  133. */
  134. a {
  135. text-decoration: none;
  136. }
  137. ol,
  138. ul {
  139. /*开发中 UI 设计的列表都是和原生的样式差太多,所以直接给取消 ol,ul 默认列表样式*/
  140. list-style: none;
  141. }
  142. button,
  143. input[type='submit'],
  144. input[type='button'] {
  145. /*鼠标经过是「小手」形状表示可点击*/
  146. cursor: pointer;
  147. }
  148. .clearfix {
  149. &:before,
  150. &:after {
  151. content: " ";
  152. display: table;
  153. }
  154. &:after {
  155. clear: both;
  156. }
  157. }
  158. .ellipsis {
  159. overflow: hidden;
  160. text-overflow: ellipsis;
  161. white-space: nowrap;
  162. word-break: break-all;
  163. word-wrap: normal
  164. }
  165. .multline-ellipsis {
  166. word-break: break-all;
  167. overflow: hidden;
  168. display: -webkit-box;
  169. text-overflow: ellipsis;
  170. -webkit-line-clamp: 2;
  171. -webkit-box-orient: vertical;
  172. }
  173. .pull-right {
  174. float: right !important;
  175. }
  176. .pull-left {
  177. float: left !important;
  178. }
  179. .hide,
  180. .hidden {
  181. display: none !important;
  182. }
  183. .show {
  184. display: block !important;
  185. }
  186. .invisible {
  187. visibility: hidden;
  188. }
  189. .vh-center {
  190. position: absolute;
  191. left: 50%;
  192. top: 50%;
  193. transform: translate(-50%, -50%);
  194. }
  195. // 适配iphoneX等刘海屏苹果手机的吸底元素(目前已知高端机型的dpr都是3)
  196. .ipx-margin-bottom {
  197. margin-bottom: calc(~"constant(safe-area-inset-bottom)");
  198. margin-bottom: calc(~"env(safe-area-inset-bottom)");
  199. }
  200. .ipx-padding-bottom {
  201. padding-bottom: calc(~"constant(safe-area-inset-bottom)");
  202. padding-bottom: calc(~"env(safe-area-inset-bottom)");
  203. }

四、边框

在移动端web开发中,UI设计稿中设置边框为1像素,前端在开发过程中如果出现border:1px,测试会发现在retina屏机型中,1px会比较粗,即是较经典的移动端1px像素问题

解决问题的方案有多种,这里主要采用 viewport + rem 的方案,同时通过设置对应viewport的rem基准值,这种方式就可以像以前一样写1px了.

<!DOCTYPE html>
<html lang="en">
<head>
    <title>移动端1px问题</title>
    <meta http-equiv="Content-Type" content="text/html;charset=UTF-8" />
    <meta name="viewport" id="WebViewport"
        content="width=device-width,initial-scale=1, maximum-scale=1, minimum-scale=1, user-scalable=no" />
    <style>
        html {
            font-size: 11px;
        }
        body {
            padding: 1rem;
        }
        * {
            padding: 0;
            margin: 0;
        }
        .item {
            padding: 1rem;
            border-bottom: 1px solid gray;
            font-size: 1.2rem;
        }
    </style>
    <script>
        var viewport = document.querySelector("meta[name=viewport]");
        var dpr = window.devicePixelRatio || 1;
        var scale = 1 / dpr;
        //下面是根据设备dpr设置viewport
        viewport.setAttribute(
            "content", +
            "width=device-width," +
            "initial-scale=" +
            scale +
            ", maximum-scale=" +
            scale +
            ", minimum-scale=" +
            scale +
            ", user-scalable=no"
        );

        var docEl = document.documentElement;
        var fontsize = 10 * (docEl.clientWidth / 320) + "px";
        docEl.style.fontSize = fontsize;
    </script>
</head>
<body>
    <div class="item">border-bottom: 1px solid gray;</div>
    <div class="item">border-bottom: 1px solid gray;</div>
</body>
</html>

优点:所有场景都能满足,一套代码,可以兼容基本所有布局。
缺点:老项目修改代价过大,只适用于新项目
**

公共集合

// Variables
@import "variables.less";

// Base CSS
@import "base.less";

// Mixins
@import "mixins.less";

// font(自定义图标)
@import "../assets/fonts/iconfont.css";

// border
@import "border.less";