分享一些在項目中好用的一些CSS輔助函數,能夠直接應用到你本身的樣式代碼中,傳送門。這些函數固然不是CSS原生寫法,有分爲sass語法和less語法兩套,你們能夠自行選擇複製或者下載。css
下面羅列的均是scss語法。less語法請查看傳送門html
%clearfix {
&:after,
&:before {
content: " ";
display: table;
}
&:after {
clear: both;
}
}
複製代碼
該函數能夠直接用來清除浮動,好比(爲了節省篇幅,後面的示例html代碼就不羅列了,請參考源碼):css3
<section class="clear-fix-demo">
<div class="container clearfix">
<div class="float-left">我是浮動的元素我是浮動的元素我是浮動的元素我是浮動的元素我是浮動的元素我是浮動的元素我是浮動的元素</div>
<div class="text">我是浮動元素旁邊的</div>
</div>
<div class="brother">我是上層容器的兄弟元素</div>
</section>
複製代碼
對應的scss:git
.clear-fix-demo{
.container{
border: 2px solid orange;
padding: 10px;
.float-left{
border: 2px dashed black;
float: left;
width: 100px;
}
.text {
border: 2px solid blue;
}
&.clearfix{
@extend %clearfix
}
}
.brother{
margin-top: 10px;
border: 2px solid palevioletred;
}
}
複製代碼
效果以下:github
// 文字溢出省略,僅webkit支持多
@mixin ellipsis($lines) {
@if ($lines == 1) {
overflow: hidden;
text-overflow: ellipsis;
white-space: nowrap;
} @else {
display: -webkit-box;
-webkit-box-orient: vertical;
-webkit-line-clamp:$lines;
overflow: hidden;
text-overflow: ellipsis;
word-break:break-all;
}
}
%ellipsis{
@include ellipsis(1);
}
複製代碼
該函數能夠用來實現一行省略或者多行省略,使用以下:web
.ellipsis-demo{
.one-line-ellipsis{
margin-top: 10px;
width: 100px;
@extend %ellipsis;
border: 2px solid peru;
}
.multiple-line-ellipsis{
margin-top: 10px;
width: 100px;
@include ellipsis(3);
border: 2px solid peru;
}
}
複製代碼
效果以下:瀏覽器
%no-scrollbar {
&::-webkit-scrollbar {
display: none !important;
width: 0 !important;
height: 0 !important;
-webkit-appearance: none;
opacity: 0 !important;
}
}
複製代碼
該函數是用來去掉滾動條的,這裏支持的是webkit內核的瀏覽器,使用以下:sass
.noscrollbar-demo{
margin-top: 10px;
overflow: scroll;
height: 100px;
width: 100px;
border: 2px solid purple;
@extend %no-scrollbar;
}
複製代碼
效果對好比下:bash
vsapp
%_onepxElement {
content: '';
position: absolute;
}
%_onepxTopBottom {
@extend %_onepxElement;
left: 0;
right: 0;
}
%_onepxLeftRight {
@extend %_onepxElement;
top: 0;
bottom: 0;
}
@mixin setDprBorder($direction: tb) {
@for $i from 1 through 4 {
@media screen and (-webkit-min-device-pixel-ratio: $i) {
@if($direction == tb){
transform: scaleY(1 / $i);
} @else if($direction == lr) {
transform: scaleX(1 / $i);
} @else if($direction == full) {
transform: scale(1 / $i);
}
}
}
}
/*
* 一像素邊框
* $direction: 邊框方向,默認底邊框
* $style: 線條樣式,默認solid
* $color: 邊框顏色
*/
@mixin one-px-border($direction: bottom, $style: solid, $color: #e5e5e5) {
position: relative;
$border: 1px $style $color;
@if ($direction == bottom) {
&:after {
@extend %_onepxTopBottom;
@include setDprBorder(tb);
border-top: $border;
bottom: 0;
}
} @else if ($direction == top) {
&:before {
@extend %_onepxTopBottom;
@include setDprBorder(tb);
border-top: $border;
top: 0;
}
} @else if ($direction == left) {
&:before {
@extend %_onepxLeftRight;
@include setDprBorder(lr);
border-left: $border;
left: 0;
}
} @else if ($direction == right) {
&:after {
@extend %_onepxLeftRight;
@include setDprBorder(lr);
border-left: $border;
right: 0;
}
}
}
// 默認下邊框
%one-px-border{
@include one-px-border();
}
// 四邊一像素邊框
@mixin full-px-border($color: #e5e5e5, $radius: 0, $zIndex: -1){
position: relative;
z-index: 1;
&:before{
content: '';
position: absolute;
z-index: $zIndex;
border: 1px solid $color;
width: 200%;
height: 200%;
border-radius: inherit;
transform: scale(.5);
transform-origin: top left;
border-radius: $radius * 2;
left: 0;
top: 0
}
}
%full-px-border{
@include full-px-border();
}
複製代碼
這個就特有用的一個函數了,在移動端上,UI小姐姐畫出的邊框線永遠是0.5px,爲了迎合小姐姐的需求,咱們既可使用這個來實現,使用以下:
.one-px-border-demo{
.bottom-border{
@include one-px-border(bottom, dashed, gold);
width: 100px;
margin-top: 10px;
}
.full-border{
@include full-px-border(gold);
width: 100px;
margin-top: 10px;
}
}
複製代碼
效果以下:
// 去除單位並返回數值
@function strip-units($number) {
@return $number / ($number * 0 + 1);
}
// px轉rem
@mixin px2rem($attr, $num, $base: 37.5) {
$list: (); //存儲全部轉換後的值
// 遍歷全部值並轉換爲rem單位
@for $i from 1 through length($num) {
// 計算單個rem值
$value: strip-units(nth($num, $i)) / $base * 1rem;
// 添加到列表中
$list: append($list, $value);
}
// 設置屬性值
#{$attr}:$list;
}
@function px2rem($num, $base: 37.5) {
@return strip-units($num) / $base * 1rem;
}
複製代碼
這個在移動端上就更有用了,推薦等級直接五顆星!!使用這個還須要搭配工具庫去計算當前頁面的rem,通常都使用淘寶的flexible.js。有了這個你就須要再使用Vscode那個什麼px2rem
插件,老實說這個插件很差用,由於它是直接將px轉換爲rem,每一個人配置的rem對應的px值不必定同樣,因此轉出來多是錯的,可是這個函數就不同了,它是能夠直接看到原始px的,因此滿分推薦。使用以下:
.px-2-rem-demo{
@include px2rem(height, 400px);
@include px2rem(width, 200px);
@include px2rem(padding, 5px 10px);
margin: px2rem(10px);
}
複製代碼
兩種使用方法都在上面寫了,效果的話就不說了,誰用誰知道。
@mixin center($position) {
position: absolute;
@if $position == 'vertical' {
top: 50%;
transform: translateY(-50%);
}
@else if $position == 'horizontal' {
left: 50%;
transform: translateX(-50%);
}
@else if $position == 'both' {
top: 50%;
left: 50%;
transform: translate(-50%, -50%);
}
}
複製代碼
該函數是用來對某個元素使用position爲絕對定位的居中法,使用以下:
.center-demo{
// 由於center函數是使用相對定位來實現的,因此其父元素記得設置relative。
position: relative;
border: 1px solid yellowgreen;
width: 400px;
height: 400px;
.both{
width: 100px;
height: 100px;
border: 1px solid goldenrod;
@include center(both)
}
.vertical{
width: 100px;
height: 100px;c
border: 1px solid goldenrod;
@include center(vertical)
}
.horizontal{
width: 100px;
height: 100px;
border: 1px solid goldenrod;
@include center(horizontal)
}
}
複製代碼
效果以下:
@mixin background-gradient($start-color, $end-color, $orientation) {
background: $start-color;
@if $orientation == 'vertical' {
background: linear-gradient(to bottom, $start-color, $end-color);
} @else if $orientation == 'horizontal' {
background: linear-gradient(to right, $start-color, $end-color);
} @else {
background: radial-gradient(ellipse at center, $start-color, $end-color);
}
}
複製代碼
該函數能夠用來作各類方向的背景漸變,使用以下:
.gradient-demo{
.vertical{
margin-top: 10px;
width: 100px;
height: 100px;
border: 1px solid grey;
@include background-gradient(rgba(255,146,156,1), rgba(216,0,0,1), vertical)
}
.horizontal{
margin-top: 10px;
width: 100px;
height: 100px;
border: 1px solid grey;
@include background-gradient(rgba(255,146,156,1), rgba(216,0,0,1), horizontal)
}
.radius{
margin-top: 10px;
width: 100px;
height: 100px;
border: 1px solid grey;
@include background-gradient(rgba(255,146,156,1), rgba(216,0,0,1), radius)
}
}
複製代碼
效果以下:
@mixin triangle($direction: down, $size: 5px, $color: #F96001) {
width: 0px;
height: 0px;
@if ($direction == left) {
border-top: $size solid transparent;
border-bottom: $size solid transparent;
border-right: $size solid $color;
}
@else if ($direction == right) {
border-top: $size solid transparent;
border-bottom: $size solid transparent;
border-left: $size solid $color;
}
@else if ($direction == down) {
border-left: $size solid transparent;
border-right: $size solid transparent;
border-top: $size solid $color;
}
@else {
border-left: $size solid transparent;
border-right: $size solid transparent;
border-bottom: $size solid $color;
}
}
複製代碼
這個函數也特有用,時不時的你就須要實現一個三角形來知足UI小姐姐的需求,使用以下:
.triangle-demo{
margin-top: 10px;
.left{
@include triangle(left, 10px);
margin-bottom: 10px;
}
.right{
@include triangle(right, 10px);
margin-bottom: 10px;
}
.down{
@include triangle(down, 10px);
margin-bottom: 10px;
}
.up{
@include triangle(up, 10px);
margin-bottom: 10px;
}
}
複製代碼
效果以下:
上面的全部代碼均沒有作瀏覽器差別屬性(vendor-prefix)的添加,這個建議統一使用插件去完成,不必人工寫一遍。若是你使用的postcss的話,可使用autoprefixer或者postcss-preset-env
該倉庫會持續更新,也歡迎各位童鞋PR,一塊兒豐富~