CSS常⽤Less公共样式封装/------------------------------------- ├布局┆└------------------------------------/
// 盒⼦宽⾼
.size(@w, @h) { width: @w; height: @h; }
// 最⼩尺⼨, 兼容IE6
.min-width(@min-w) { min-width: @min-w; _width: @min-w; }
.min-height(@min-h) { min-height: @min-h; _height: @min-h; }
// 内联块级元素, 兼容IE6
.dib() { display: inline-block; *display: inline; *zoom: 1; }
// 固定定位, 兼容IE6
.fixed() { position: fixed; _position: absolute; *zoom: 1; }
// 统⼀盒模型
.
border-box() {
*, *:after, *:before { -webkit-box-sizing: border-box; -moz-box-sizing: border-box; box-sizing: border-box; }
}
// ⽂字图⽚居中
.center(text-x) { text-align: center; }
.center(text-y) { display: table-cell; vertical-align: middle; }
// 块级元素⽔平居中
.center(auto-x) { display: block; margin-left: auto; margin-right: auto; }
// 居中, 不确定尺⼨, 不兼容 IE6
.center(unknown) { position: absolute; top: 0; left: 0; right: 0; bottom: 0; margin: auto; }
.center(unknown-x) { position: absolute; left: 0; right: 0; margin-left: auto; margin-right: auto; }
.center(unknown-y) { position: absolute; top: 0; bottom: 0; margin-top: auto; margin-bottom: auto; }
borderbox// 居中, 确定尺⼨, 兼容 IE6
.center(known, @w, @h) {
.size(@w, @h);
position: absolute; top: 50%; left: 50%; margin-top: -(@w / 2); margin-left: -(@h / 2);
}
.center(known-x, @w) {
width: @w;
position: absolute; left: 50%; margin-left: -(@h / 2);
}
.center(known-y, @h) {
height: @h;
position: absolute; top: 50%; margin-top: -(@w / 2);
}
// 居中, CSS3 平移⽅式, 兼容性不⾏
.center(translate) { position: absolute; top: 50%; left: 50%; transform: translate(-50%, -50%); }
// 居中, Flex ⽅式, 兼容性不⾏
.center(flex) { display: flex; align-items: center; justify-content: center; }
// 多个⼦项布局
.list(float, @w: 25%) { float: left; width: @w; }
.list(inline, @w: 25%) { .dib(); width: @w; }
.list(flex) { flex: 1; }
/
/ 遮罩层, 全屏遮罩、区域遮罩
.over-screen(fixed) { .fixed(); top: 0; left: 0; right: 0; bottom: 0; }
.over-screen(absolute) { position: absolute; top: 0; left: 0; right: 0; bottom: 0; }
// 容器宽⾼⽐固定
// 100* 1/1 = 100%
// 100* 3/4 = 75%
.fixed-ratio(@padding-top: 100%) {
position: relative; width: 100%; height: 0; padding-top: @padding-top;
img { position: absolute; top: 0; left: 0; width: 100%; height: 100%; }
}
// 扩展点击区域
.
extend-click() {
position: relative;
&:before { content: ''; position: absolute; top: -10px; left: -10px; right: -10px; bottom: -10px; } }
// 定宽居中页⾯布局
.layout-page(@width: 1200px) { width: @width; margin-left: auto; margin-right: auto; }
// 侧边栏
// 主要区域:overflow: hidden; margin-left: xx; margin-right: xx;
.sidebar(left, @width) { position: absolute; top: 0; left: 0; width: @width; }
.sidebar(right, @width) { position: absolute; top: 0; right: 0; width: @width; }
/------------------------------------- ├字体┆└------------------------------------/
// 字体⼤⼩
.
fz(@fz) { font-size: @fz; }
// 字体⼤⼩与⾏⾼
.fz(@fz, @lh) { font-size: @fz; line-height: @lh; }
// 字体⼤⼩、⾏⾼、⾼度
.fz(@fz, @h, @lh: @h) { font-size: @fz; height: @h; line-height: @lh; }
// ⾏⾼与⾼度
.lh(@h, @lh: @h) { height: @h; line-height: @lh; }
// 字体颜⾊, 包括链接与⾮链接
.color(@color) { color: @color;}
// 字体颜⾊ + ⾃⾝ Hover
.color(@color, @hovercolor) {
color: @color;
&:hover { color: @hovercolor; }
}
// 字体颜⾊ + 链接 Hover
.color(@color, @acolor, @hovercolor) {
color: @color;
a {
color: @acolor;
&:hover { color: @hovercolor; }
}
}
/
/ 正常字体样式
.normal-font() { font-weight: normal; font-style: normal; }
// 辅助性⽂字(灰⾊)
.assist-font(@color: #b0b0b0, @fz: 14px) { color: @color; font-size: @fz; }
// 禁⽌换⾏, ⽂本溢出省略号显⽰ (⼀⾏)
.ellipsis() {
white-space: normal; word-wrap: break-word; word-break: break-all;
-o-text-overflow: ellipsis; -ms-text-overflow: ellipsis; text-overflow:ellipsis; overflow:hidden; }
}
// ⽂本溢出省略号显⽰ (多⾏)
// 只⽀持 webkit 浏览器, 解决⽅案:⾼度 = ⾏⾼*⾏数
/
/ height: 90px; line-height: 30px; -webkit-line-clamp: 3;
.ellipsis-mult(@n: 3) {
display: -webkit-box; -webkit-box-orient: vertical;-webkit-line-clamp: @n; word-break: break-all; -o-text-overflow: ellipsis; -ms-text-overflow: ellipsis; text-overflow:ellipsis; overflow: hidden;
}
// 书写模式:牌匾从右⾄左⽔平单⾏排版效果、⽂笺从右⾄左、从上⾄下排版效果
.retext(x) { direction: rtl; unicode-bidi: bidi-override; }
.retext(y) { writing-mode: tb-rl; writing-mode: vertical-rl; }
// ⽂字透明
.transparent-text() { font: 0/0 serif; text-shadow: none; color: transparent; }
// ⽂字隐藏(常⽤于SEO优化)
//
xx
.hidden-text() { text-indent : -9999px; overflow: hidden; text-align: left; }
// ⽂字外发光效果
.glow-text(@r: 10px, @color: gold) { text-shadow: 0 0 @r @color; }
/------------------------------------- ├图像┆└------------------------------------/
// ⽤ max-width 来防⽌图⽚撑破容器
.max-img() { display: block; max-width: 100%; height: auto; }
// 2x 3x 背景图⽚
.bg-image(@url) {
background-image: url("@url + '@2x.png'");
@media (-webkit-min-device-pixel-ratio: 3), (min-device-pixel-ratio: 3) {
background-image: url("@url + '@3x.png'");
}
}
// 全屏⼤图背景
.fullscreen-bg(@url) {
width: 100vw;
height: 100vh;
background: url(@url) no-repeat 50% 50%;
background-size: cover;
}
// 滤镜: 将彩⾊照⽚显⽰为⿊⽩照⽚
.
grayscale() {
filter: grayscale(100%);
-webkit-filter: grayscale(100%);
-moz-filter: grayscale(100%);
-ms-filter: grayscale(100%);
-o-filter: grayscale(100%);
}
/------------------------------------- ├动效┆└------------------------------------/
// 链接默认⽆下划线,hover后有下划线的样式
.hover-link() {
text-decoration: none;
&:hover { text-decoration: underline; }
}
// 将链接变成默认的⽂字样式
// 将链接变成默认的⽂字样式
.unstyled-link() {
color: inherit;
cursor: inherit;
text-decoration: inherit;
&:active, &:focus { outline: none; }
}
// 盒⼦阴影
/
/ box-shadow: ⽔平阴影的位置, 垂直阴影的位置, 模糊距离, 阴影的⼤⼩, 阴影的颜⾊, 阴影开始⽅向(默认是从⾥往外,设置inset就是从外往⾥); // box-shadow: h-shadow v-shadow blur spread color inset;
.box-shadow() {
box-shadow: 0px 14px 26px 0px rgba(0, 0, 0, 0.1);
}
// 盒⼦ Hover
.box-hover() {
// box-shadow: 0px 1px 2px 0px rgba(84, 107, 107, .4);
transition: all .2s linear;
&:hover {
box-shadow: 0 15px 30px rgba(0, 0, 0, .1);
transform: translate3d(0, -2px, 0);
}
}
.box-hover2() {
transition: transform .5s ease;
&:hover {
transform: translateX(10px);
}
}
// 三维闪动 bug 处理
.transform-fix() { -webkit-backface-visibility: hidden; -webkit-transform-style: preserve-3d; }
/
/ Animation
.ani(@name, @time: 1s, @ease: ease-in-out, @fillmode: forwards) {
animation-name: @name;
animation-duration: @time;
animation-timing-function: @ease;
animation-fill-mode: @fillmode;
}
/------------------------------------- ├功能┆└------------------------------------/
// 浮动, 兼容 IE6
.fl() { float: left; *display: inline; _display:inline; }
.fr() { float: right; *display: inline; _display:inline; }
/
/ 清除浮动
.clearfix() {
*zoom: 1;
&:after { display: block; clear: both; content: ''; visibility: hidden; height: 0; }
}
.clearfix(table) {
*zoom: 1;
&:before, &:after { content: " "; display: table; clear: both; }
}
// 禁⽌⽂本被选择
.user-select() { -webkit-user-select: none; -moz-user-select: none; -ms-user-select: none; user-select: none; }
// 隐藏⿏标⼿势
.hide-cursor() { cursor: none !important; }
// ⿏标禁⽤样式,但仍然可以触发事件
// ⿏标禁⽤样式,但仍然可以触发事件
//
.disabled() { cursor: not-allowed; }
// 禁⽤元素事件
// 1. 阻⽌任何点击动作的执⾏
// 2. 使链接显⽰为默认光标(cursor:default)
// 3. 阻⽌触发hover和active状态
// 4. 阻⽌JavaScript点击事件的触发
.
pointer-events() { pointer-events: none; }
// 模糊
.blur(@blur: 10px) {
filter: blur(@blur);
-webkit-filter: blur(@blur);
-moz-filter: blur(@blur);
-o-filter: blur(@blur);
-ms-filter: blur(@blur);
filter: progid:DXImageTransform.Microsoft.Blur(PixelRadius='@{blur}');
*zoom: 1;
}
/
/ 透明度, 兼容 IE8
.opacity(@opacity: 20) { opacity: @opacity / 100; filter: alpha(opacity=@opacity); }
// ⽤伪类来显⽰打印时 a 标签的链接
.print-link() {
@media print {
a[href]:after { content: " (" attr(href) ") "; }
}
}
// 隔⾏换⾊
.zebra-lists(odd, @color) {
&.odd {
>li:nth-child(odd) { background-color: @color; }
}
}
.zebra-lists(even, @color) {
&.even {
>li:nth-child(even) { background: green; }
}
}
// ⾸字下沉
.first-letter(@font-size: 6em) {
&::first-letter{
float: left;
line-height: 1;
font-size: @font-size;
}
}
// 特殊标记段落第⼀⾏
.first-line() {
&::first-line{
color: red
}
}
/
/ 美化选中⽂本
.beauty-select() {
&::selection{
color: #fff;
background-color: #6bc30d;