CSS学习培训笔记之常见Mixin封裝案例编码

2020-10-30 12:58 jianzhan

序言

以便合理的维护保养和开发设计新项目,编码的反复运用就显得尤其关键。在Sass中,除@import和@extend可使你的编码更为具备反复运用性,@mixin命令也一样能提升你编码的反复应用率并简化你的编码。本人在做新项目的全过程中总结梳理的1些常见Mixin,如今共享出来供大伙儿学习培训。

根据Less撰写。可是CSS预解决器全是相通的,你能够很随便的改动成Sass、Stylus

编码在所难免不正确和Bug,假如你能帮我改动或填补 万分谢谢!!

参照了1些开源系统库,如:est、csslab等

Usage

/**
* 著作:mixin.less
* 升级:2017年12月14日
* 简介:1. 1个根据 Less 的款式专用工具库,封裝了常见 mixin,协助您更轻轻松松地撰写 Less 编码。
*       2. 只在启用时才輸出编码,降低编码冗余,防止款式污染
*       3. 不自带适配前缀,降低编码量,而提议选用专用工具转化成,如 postcss、Autoprefixer、less-plugin-autoprefix 等
*       4. 附带 IE 各类 Hack
*/
/*-------------------------------------
├   合理布局                              ┆
└------------------------------------*/
// 盒子宽高
.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; }
// 统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; }
// 垂直居中, 明确规格, 适配 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(⑸0%, ⑸0%); }
// 垂直居中, 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: ⑴0px; left: ⑴0px; right: ⑴0px; bottom: ⑴0px; }
}
// 定宽垂直居中网页页面合理布局
.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; }
// 严禁换行, 文字外溢省略号显示信息 (1行)
.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提升)
// <a href="" title="Logo SEO 提升 "><h1 class="logo">xx</h1></a>
.hidden-text() { text-indent : ⑼999px; 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, ⑵px, 0);
  }
}
.box-hover2() {
  transition: transform .5s ease;
  &:hover {
    transform: translateX(10px);
  }
}
// 3维闪烁 bug 解决
.transform-fix() { -webkit-backface-visibility: hidden; -webkit-transform-style: preserve⑶d; }
// 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; }
// 电脑鼠标禁用款式,但依然能够开启恶性事件
// <input type="text" disabled="disabled">
.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;
  }
}
// 独特标识段落第1行
.first-line() {
  &::first-line{
    color: red
  }
}
// 清理选定文字
.beauty-select() {
  &::selection{
   color: #fff;
   background-color: #6bc30d;
   text-shadow: none;
  }
}
// 清理占位符 placeholder 款式
.beauty-placeholder(@fz, @color: #999, @align: left) {
  &:-moz-placeholder { font-size: @fz; color: @color; text-align: @align; }
  &:-ms-input-placeholder { font-size: @fz; color: @color; text-align: @align; }
  &::-webkit-input-placeholder { font-size: @fz; color: @color; text-align: @align; }
}
// 清理占位符 placeholder 款式(自定特性和值)
.beauty-placeholder(custom, @property, @value) {
  &:-moz-placeholder { @{property}: @value; }
  &:-ms-input-placeholder { @{property}: @value; }
  &::-webkit-input-placeholder { @{property}: @value; }
}
/*-------------------------------------
├   图型                              ┆
└------------------------------------*/
// 3角形
.triangle(@width: 4px,@color: #000) {
  display: inline-block;
  width: 0;
  height: 0;
  vertical-align: middle;
  border-top: @width solid @color;
  border-left: @width solid transparent;
  border-right: @width solid transparent;
}
// 3角形箭头气泡实际效果, IE6⑺ 无主要表现
.arrow(top, @w: 10px, @color, @x: 50%) {
  position: relative;
  &:before { position: absolute; bottom: 100%; left: @x; content: " "; height: 0; width: 0; pointer-events: none; border-style: solid; border-color: transparent; border-bottom-color: @color; border-width: unit(@w, px); @margin: -@w; margin-left: unit(@margin, px); }
}
.arrow(right, @w: 10px, @color, @y: 50%) {
  position: relative;
  &:before { position: absolute; left: 100%; top: @y; content: " "; height: 0; width: 0; pointer-events: none; border-style: solid; border-color: transparent; border-left-color: @color; border-width: unit(@w, px); @margin: -@w; margin-top: unit(@margin, px); }
}
.arrow(bottom, @w: 10px, @color, @x: 50%) {
  position: relative;
  &:before { position: absolute; top: 100%; left: @x; content: " "; height: 0; width: 0; pointer-events: none; border-style: solid; border-color: transparent; border-top-color: @color; border-width: unit(@w, px); @margin: -@w; margin-left: unit(@margin, px); }
}
.arrow(left, @w: 10px, @color, @y: 50%) {
  position: relative;
  &:before { position: absolute; right: 100%; top: @y; content: " "; height: 0; width: 0; pointer-events: none; border-style: solid; border-color: transparent; border-right-color: @color; border-width: unit(@w, px); @margin: -@w; margin-top: unit(@margin, px); }
}
// 3角形箭头气泡实际效果, 带边框
.arrow-with-border(top, @w: 10px, @color, @border-w: 1px, @border-color, @x: 50%) {
  position: relative;
  &:before, &:after { bottom: 100%; left: @x; content: " "; height: 0; width: 0; position: absolute; pointer-events: none; border-style: solid; border-color: transparent; }
  &:after { border-bottom-color: @color; border-width: unit(@w, px); @margin: -@w; margin-left: unit(@margin, px); }
  &:before { border-bottom-color: @border-color; @arrbo: @w+@border-w; border-width: unit(@arrbo, px); @margin-bo: -@arrbo; margin-left: unit(@margin-bo, px); }
}
.arrow-with-border(bottom, @w: 10px, @color, @border-w: 1px, @border-color, @x: 50%) {
  position: relative;
  &:before, &:after { top: 100%; left: @x; content: " "; height: 0; width: 0; position: absolute; pointer-events: none; border-style: solid; border-color: transparent; }
  &:after { border-top-color: @color; border-width: unit(@w, px); @margin: -@w; margin-left: unit(@margin, px); }
  &:before { border-top-color: @border-color; @arrbo: @w+@border-w; border-width: unit(@arrbo, px); @margin-bo: -@arrbo; margin-left: unit(@margin-bo, px); }
}
.arrow-with-border(left, @w: 10px, @color, @border-w: 1px, @border-color, @y: 50%) {
  position: relative;
  &:before, &:after { top: @y; right: 100%; content: " "; height: 0; width: 0; position: absolute; pointer-events: none; border-style: solid; border-color: transparent; }
  &:after { border-right-color: @color; border-width: unit(@w, px); @margin: -@w; margin-top: unit(@margin, px); }
  &:before { border-right-color: @border-color; @arrbo: @w+@border-w; border-width: unit(@arrbo, px); @margin-bo: -@arrbo; margin-top: unit(@margin-bo, px); }
}
.arrow-with-border(right, @w: 10px, @color, @border-w: 1px, @border-color, @y: 50%) {
  position: relative;
  &:before, &:after { top: @y; left: 100%; content: " "; height: 0; width: 0; position: absolute; pointer-events: none; border-style: solid; border-color: transparent; }
  &:after { border-left-color: @color; border-width: unit(@w, px); @margin: -@w; margin-top: unit(@margin, px); }
  &:before { border-left-color: @border-color; @arrbo: @w+@border-w; border-width: unit(@arrbo, px); @margin-bo: -@arrbo; margin-top: unit(@margin-bo, px); }
}
/*-------------------------------------
├   组件                              ┆
└------------------------------------*/
// 吸顶导航栏
.fix-header(@h: 70px) {
  .fixed();
  top: 0;
  left: 0;
  width: 100%;
  height: @h;
  z-index: 1000;
  // background-color: rgba(256, 256, 256, .92);
  // border-bottom: 1px solid rgba(7, 17, 27, 0.1);
  // box-shadow: 0px 0px 20px rgba(0,0,0,0.2);
}
// 吸底导航栏
.fix-header(@h: 70px) {
  .fixed();
  left: 0;
  bottom: 0;
  width: 100%;
  height: @h;
  z-index: 1000;
}
// 键入框
.input-text() {
  display: block;
  width: 100%;
  padding: 4px 8px;
  font-size: 14px;
  line-height: 1.42858;
  color: #333;
  border: 1px solid #ddd;
  background-color: #fff;
  border-radius: 3px;
}
// 切分线
// <span class="separator">|/-</span>
.separator() {
  margin: 0 10px;
  color: #999;
  font-size: 14px;
}
// 切分线 / (面包屑导航栏)
.separator2() {
  &:before {
    padding: 0 5px;
    color: #ccc;
    content: "/\00a0";
  }
}
// <hr class="hr">
// 付款宝:我也是有底线的
.hr() {
  height: 1px;
  margin: 10px 0;
  border: 0;
  clear: both;
  background-color: #e2e2e2;
}
// 改装的 fieldset
// <fieldset><legend>返璞归真</legend></fieldset>
.fieldset() {
  border-color: #d2d2d2;
  border-width: 1px 0 0;
  border-style: solid;
  legend {
    padding: 0 20px;
    text-align: center;
    font-size: 20px;
    font-weight: 300;
  }
}
// 引入区块(效仿 Layui)
// <div class="blockquote">Lorem ipsum dolor sit amet.</div>
.blockquote() {
  margin-bottom: 10px;
  padding: 15px;
  line-height: 22px;
  border-left: 5px solid #009688;
  border-radius: 0 2px 2px 0;
  background-color: #f2f2f2;
}
// 徽章 (椭圆、小圆点)
// <span class="badge">10</span>
.badge(...) {
  position: relative;
  display: inline-block;
  font-size: 12px;
  color: #fff;
  background-color: #FF5722;
}
.badge(ellipse) {
  min-width: 8px;
  height: 18px;
  padding: 2px 6px;
  text-align: center;
  line-height: 18px;
  border-radius: 9px;
}
.badge(dot) {
  width: 8px;
  height: 8px;
  border-radius: 50%;
}
// 关掉按钮
// <button class="close" type="button"><span>&times;</span></button>
.close() {
  position: relative;
  -webkit-appearance: none;
  padding: 0;
  cursor: pointer;
  background: 0 0;
  border: 0;
  font-size: 20px;
  font-weight: 700;
  line-height: 1;
  color: #000;
  text-shadow: 0 1px 0 #fff;
  filter: alpha(opacity=20);
  opacity: .2;
  &:hover {
    color: #000;
    text-decoration: none;
    cursor: pointer;
    filter: alpha(opacity=50);
    opacity: .5;
  }
  &:before {
    content: '';
    position: absolute;
    top: ⑴0px;
    left: ⑴0px;
    right: ⑴0px;
    bottom: ⑴0px;
  }
}
// 1 像素边框难题
.onepx(...) {
  position: relative;
  &:after {
    content: '';
    display: block;
    position: absolute;
    left: 0;
    width: 100%;
    border-top: 1px solid rgba(7, 17, 27, 0.1);
    transform: scaleY(0.5);
  }
}
.onepx(top) { &:after { top: 0; } }
.onepx(bottom) { &:after { bottom: 0; } }
.onepx-easy(top, @color: #ccc) { box-shadow: inset 0px ⑴px 1px ⑴px @color; }
.onepx-easy(bottom, @color: #ccc) { box-shadow: inset 0px 1px 1px ⑴px @color; }

总结

以上便是这篇文章内容的所有內容了,期待本文的內容对大伙儿的学习培训或工作中具备1定的参照学习培训使用价值,假如有疑惑大伙儿能够留言沟通交流,感谢大伙儿对脚本制作之家的适用。