前言
为了确保项目样式的可维护性和拓展性,我们一般在项目开始,跟ui设计确定一些公共设计规范,比如font-size、font-family、color等等,来统一项目的公共样式。
一、变量
//
// variables.less
// --------------------------------------------------
// font
// --------------------------------------------------
@font-family-default: 'Helvetica Neue', Helvetica, 'PingFang SC', 'Hiragino Sans GB', 'Microsoft Yahei', Arial, sans-serif;
@font-size-title-default: 18px;
@font-size-master: 16px;
@font-size-main: 14px;
@font-size-default: 12px;
@font-size-secondary: 10px;
@font-weight-bold: 600;
@font-weight: 500;
@font-weight-light: 400;
@line-height-default: 1;
// Colors
// --------------------------------------------------
// 通用
@color-white: #FFF;
@color-blue: #3969FE;
@color-red: #F23D20;
@color-gray: #999;
@color-warn: #FA553C;
// 背景色
@color-bg-white: @color-white;
@color-bg-gray: #F8F8F8;
@color-bg-gray2: #FAFAFA;
@color-bg-gray-light: #F5F5F5;
@color-bg-gray-dark: #F8F9FC;
@color-bg-gray-dark2: #F1F1F1;
@color-bg-blue: @color-blue;
@color-bg-blue-light: #F2F6FF;
@color-bg-blue-dark: #4E79FF;
@color-bg-red-light:rgba(250,85,60,0.05);
// 边框
@color-border-grey: rgba(0,0,0,0.05);
// 字体颜色
@color-text: #333 ; //文案色
@color-text-gray-light: #666; // 浅灰色文案
@color-text-gray: @color-gray; //灰色文案
@color-text-white: @color-white; //有背景色的文案
@color-text-blue: @color-blue;
@color-text-green: #129352;
@color-text-red: @color-red; // 预警的颜色
@color-text-red-assistant: #FA553C;
@color-text-black: #000;
// 分割线
@color-split-10: rgba(0,0,0,0.1);
@color-split-20: rgba(0,0,0,0.2);
@color-split: rgba(0,0,0,0.05); //分割线的颜色
@color-split2: rgba(0,0,0,0.07); //分割线的颜色
// 多弹窗层级问题 z-index
@navbar-z-index: 1000;
@tabbar-z-index: 1000;
@backtop-z-index: 1100;
@search-z-index: 1200;
@product-z-index: 1200;
@search-popup-z-index: @search-z-index + 10;
@category-popup-z-index: @search-z-index - 10;
// componets
// --------------------------------------------------
// chart
@color-chart-blue: #5374E8;
@color-chart-green: #10AC84;
@color-chart-tip-bg: #243850;
// popup
@popup-background-color: @color-white;
@popup-transition-duration: .2s;
// overlay
@overlay-background-color: rgba(0, 0, 0, .4);
// toast
@toast-font-color: @color-white;
@toast-max-width: 70%;
@toast-border-radius: 7px;
@toast-container-padding: 24px;
@toast-message-padding-top: 8.5px;
@toast-background-color: rgba(0, 0, 0, .7);
@toast-icon-font-size: 40px;
@toast-default-width: 148px;
@toast-default-min-height: 137px;
@toast-default-padding: 15px;
@toast-default-font-size: 14px;
@toast-default-message-padding-top: 19px;
@toast-text-min-width: 104px;
@toast-text-font-size: 16px;
@toast-text-padding: 15px 20px;
@toast-position-top-distance: 50px;
@toast-position-bottom-distance: 50px;
二、混合宏函数
//
// mixins.less
// --------------------------------------------------
// special font-family
// --------------------------------------------------
@import 'variables.less';
.PingFangSC-Light() {
font-family: PingFangSC-Light;
}
.PingFangSC-Regular() {
font-family: PingFangSC-Regular;
font-weight: @font-weight-light;
}
.PingFangSC-Medium() {
font-family: PingFangSC-Medium;
font-weight: @font-weight;
}
.PingFangSC-Semibold() {
font-family: PingFangSC-Semibold;
font-weight: @font-weight-bold;
}
.rem($px) {
@return ($px / $vw_fontsize ) * 1rem;
}
// flex
// --------------------------------------------------
// 垂直居中
.flex-col-center() {
display: flex;
flex-flow: row nowrap;
align-items: center;
}
// 两侧
.flex-between() {
display: flex;
flex-flow: row nowrap;
align-items: center;
justify-content: space-between;
}
三、基础
//
// base.less
// --------------------------------------------------
html {
/*滚动事件发生在 html 元素上;JS 中可以监听 html 的滚动*/
overflow-y: auto;
/*让 html 和浏览器窗口高度一致*/
height: 100%;
line-height: 1;
/*少数浏览器默认背景色为浅灰色,所以设置默认背景颜色为纯白*/
background-color: @color-bg-white;
-webkit-font-smoothing: antialiased;
-moz-osx-font-smoothing: grayscale;
-webkit-text-size-adjust: 100%;
}
html,
body {
/*body 宽度大 html 度时,某些浏览器会出现内部滚动条;所以设置「html、body」宽度相同且「overflow-x: hidden」*/
overflow-x: hidden;
width: 100%;
padding-bottom: constant(safe-area-inset-bottom);//为底下圆弧的高度 34px
}
body {
/*设置基本字体配置*/
font-size: @font-size-main;
/*让绝对定位元素,根据 body 定位*/
position: relative;
/*设置网页基本字体颜色为浅灰色*/
color: @color-text-gray-light;
/*使字体渲染更顺滑*/
-webkit-font-smoothing: antialiased;
-moz-osx-font-smoothing: grayscale;
text-rendering: optimizeLegibility;
-webkit-overflow-scrolling: touch;
/* body 也增加最大最小宽度限制,避免默认100%宽度的 block 元素跟随 body 而过大过小 */
max-width: 540px;
min-width: 320px;
}
#app {
font-size: initial;//重置页面字体大小恢复为浏览器默认16px,
}
// Use box sizing on all the things!
* {
box-sizing: border-box;
-webkit-tap-highlight-color: rgba(0, 0, 0, 0);
-webkit-touch-callout: none;
}
body,
p,
h1,
h2,
h3,
h4,
h5,
h6,
dl,
dd,
ul,
ol,
th,
td,
button,
figure,
input,
textarea,
form {
margin: 0;
padding: 0;
}
/**
* 不同浏览器的input、select、textarea的盒子模型宽度计算方式不同,统一为最常见的 content-box
*/
input,
select,
textarea {
-webkit-box-sizing: content-box;
-moz-box-sizing: content-box;
box-sizing: content-box;
}
table {
/*table相邻单元格的边框间的距离设置为 0*/
border-spacing: 0;
/*默认情况下给tr 设置 border 没有效果,如果 table 设置了边框为合并模式:「border-collapse: collapse;」就可以了*/
border-collapse: collapse;
}
/**
* 移除浏览器部分元素的默认边框
* acronym、fieldset … 等其他标签不是很常用,就不会一一列举;如果项目中用到,可以自己单独写
*/
img,
input,
button,
textarea {
border: none;
-webkit-appearance: none;
}
input {
/*由于 input 默认不继承父元素的居中样式,所以设置:「text-align: inherit」*/
text-align: inherit;
}
textarea {
/*textarea 默认不可以放缩*/
resize: none;
}
/**
* 由于以下元素的部分属性没有继承父节点样式,所以声明这些元素的这些属性为父元素的属性
* 取消这些元素 `outline` 样式
*/
a,
h1,
h2,
h3,
h4,
h5,
h6,
input,
select,
button,
option,
textarea,
optgroup {
font-family: inherit;
font-size: inherit;
font-weight: inherit;
font-style: inherit;
line-height: inherit;
color: inherit;
outline: none;
}
/**
* 取消超链接元素的默认文字装饰
* 另外 del、ins 标签的中划线、下划线还是挺好的,就不去掉
*/
a {
text-decoration: none;
}
ol,
ul {
/*开发中 UI 设计的列表都是和原生的样式差太多,所以直接给取消 ol,ul 默认列表样式*/
list-style: none;
}
button,
input[type='submit'],
input[type='button'] {
/*鼠标经过是「小手」形状表示可点击*/
cursor: pointer;
}
.clearfix {
&:before,
&:after {
content: " ";
display: table;
}
&:after {
clear: both;
}
}
.ellipsis {
overflow: hidden;
text-overflow: ellipsis;
white-space: nowrap;
word-break: break-all;
word-wrap: normal
}
.multline-ellipsis {
word-break: break-all;
overflow: hidden;
display: -webkit-box;
text-overflow: ellipsis;
-webkit-line-clamp: 2;
-webkit-box-orient: vertical;
}
.pull-right {
float: right !important;
}
.pull-left {
float: left !important;
}
.hide,
.hidden {
display: none !important;
}
.show {
display: block !important;
}
.invisible {
visibility: hidden;
}
.vh-center {
position: absolute;
left: 50%;
top: 50%;
transform: translate(-50%, -50%);
}
// 适配iphoneX等刘海屏苹果手机的吸底元素(目前已知高端机型的dpr都是3)
.ipx-margin-bottom {
margin-bottom: calc(~"constant(safe-area-inset-bottom)");
margin-bottom: calc(~"env(safe-area-inset-bottom)");
}
.ipx-padding-bottom {
padding-bottom: calc(~"constant(safe-area-inset-bottom)");
padding-bottom: calc(~"env(safe-area-inset-bottom)");
}
四、边框
在移动端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";