ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

常用的scss函数(mixin)

2021-07-13 11:02:15  阅读:303  来源: 互联网

标签:scss direction 函数 color height width mixin null


 


scss自出来之后,广受欢迎,如何能快速写出想要的css呢,可自定义一些scss方法,本人罗列了一些最近用到的scss函数,其实包括文本超出范围的格式化、弹性盒子居中、左浮动、右浮动、鼠标上移样式改变等

1、文本超出范围,显示省略号

/*文本格式化,超出范围,显示省略号*/
@mixin textOverflow($width:100%,$display:block) {
width: $width;
display: $display;
white-space: nowrap;
-ms-text-overflow: ellipsis;
text-overflow: ellipsis;
overflow: hidden;
}
调用@include textOverflow();也可以自己传入参数,display为block或inline-block才能达到预期的效果

生成的css:

width: 100%;
display: block;
white-space: nowrap;
-ms-text-overflow: ellipsis;
text-overflow: ellipsis;
overflow: hidden;
2、 清除浮动 ,bootstrap有类似的类名,当然也可以用overflow:hidden

/* 清除浮动 */
@mixin clearfix {
&:after {
clear: both;
content: '.';
display: block;
height: 0;
line-height: 0;
overflow: hidden;
}
*height: 1%;
}
生成的css

.demo {
*height: 1%;
}

.demo:after {
clear: both;
content: '.';
display: block;
height: 0;
line-height: 0;
overflow: hidden;
}
其他的我直接贴出函数,若有不明白如何调用的,欢迎留言


/*弹性盒子(传入null不设置该属性)*/
@mixin flexBox($direction: row, $justify: null, $align: null, $flex-wrap: null) {
display: flex;
@if ($direction!=null) {
flex-direction: $direction;
}
@if ($justify!=null) {
justify-content: $justify;
}
@if ($align!=null) {
align-items: $align;
}
@if ($flex-wrap != null) {
flex-wrap: $flex-wrap;
}
}

/*绝对定位 参数顺序:上右下左*/
@mixin positionAbsolute($top:null,$right:null,$bottom:null,$left:null) {
position: absolute;
@if ($left!="" & & $left!=null) {
left: $left;
}
@if ($right!="" & & $right!=null) {
right: $right;
}
@if ($top!="" & & $top!=null) {
top: $top;
}
@if ($bottom!="" & & $bottom!=null) {
bottom: $bottom;
}
}

/*左浮动*/
@mixin float-left($width:19%,$margin-right:1.2%) {
width: $width;
float: left;
@if ($margin-right!=null) {
margin-right: $margin-right;
}
}

/*右浮动*/
@mixin float-Right($width:19%,$margin-left:1.2%) {
width: $width;
float: right;
@if ($margin-left!=null) {
margin-left: $margin-left;
}
}

/*渐变(从上到下)*/
@mixin linear-gradient($direction:bottom,$color1:transparent,$color2:#306eff,$color3:transparent) {
//background: -webkit-linear-gradient($direction,$colorTop, $colorCenter, $colorBottom); /* Safari 5.1 - 6.0 */
background: -o-linear-gradient($direction, $color1, $color2, $color3); /* Opera 11.1 - 12.0 */
background: -moz-linear-gradient($direction, $color1, $color2, $color3); /* Firefox 3.6 - 15 */
background: linear-gradient(to $direction, $color1, $color2, $color3); /* 标准的语法 */
}

/* 行高 */
@mixin line-height($height:30px,$line-height:30px) {
@if ($height != null) {
height: $height;
}
@if ($line-height!=null) {
line-height: $line-height;
}
}

/* 画三角形 */
@mixin triangle($width:10px,$direction:top,$color:$bgBlueLight) {
border: $width solid transparent;
@if ($direction == top) { // 上三角
border-bottom-color: $color;
}
@if ($direction == bottom) { // 下三角
border-top-color: $color;
}
@if ($direction == left) { // 左三角
border-right-color: $color;
}
@if ($direction == right) { // 右三角
border-left-color: $color;
}
}

/* 文本阴影 */
@mixin text-show($h-shadow:0px, $v-shadow:0px, $blur:10px, $color:rgba(0,180,255,0.7)) {
text-shadow: $h-shadow $v-shadow $blur $color;
}

/* 链接样式 */
@mixin hoverStyle($style:(color:#d9fdff),$hoverStyle:(color:#306eff)) {
text-decoration: none;
@each $key, $value in $style {
#{$key}: #{$value};
}
@if ($hoverStyle!=null & & $hoverStyle!="") {
&:hover {
@each $key, $value in $hoverStyle {
#{$key}: #{$value};
}
}
}
}

/* 定义滚动条样式 圆角和阴影不需要则传入null */
@mixin scrollBar($width:10px,$height:10px,$outColor:$bgColor,$innerColor:$bgGrey,$radius:5px,$shadow:null) {
/*定义滚动条高宽及背景 高宽分别对应横竖滚动条的尺寸*/
&::-webkit-scrollbar {
width: $width;
height: $height;
background-color: $outColor;
}

/*定义滚动条轨道 内阴影+圆角*/
&::-webkit-scrollbar-track {
@if ($shadow!=null) {
-webkit-box-shadow: $shadow;
}
@if ($radius!=null) {
border-radius: $radius;
}
background-color: $outColor;
}

/*定义滑块 内阴影+圆角*/
&::-webkit-scrollbar-thumb {
@if ($shadow!=null) {
-webkit-box-shadow: $shadow;
}
@if ($radius!=null) {
border-radius: $radius;
}
background-color: $innerColor;
border: 1px solid $innerColor;
}
}

/* css3动画 默认3s宽度到200px */
@mixin animation($from:(width:0px),$to:(width:200px),$name:mymove,$animate:mymove 2s 1 linear infinite) {
-webkit-animation: $animate;
-o-animation: $animate;
animation: $animate;
@keyframes #{$name}
{
from {
@each $key, $value in $from {
#{$key}: #{$value};
}
}
to {
@each $key, $value in $to {
#{$key}: #{$value};
}
}
}

@-webkit-keyframes #{$name}
{
from {
@each $key, $value in $from {
$key: $value;
}
}
to {
@each $key, $value in $to {
$key: $value;
}
}
}
}

/* 圆形盒子 */
@mixin circle($size: 11px,$bg: #fff) {
border-radius: 50%;
width: $size;
height: $size;
line-height: $size;
text-align: center;
background: $bg;
}

/* placeholder */
@mixin placeholder($color: #bbb) {
// Firefox
&::-moz-placeholder {
color: $color;
opacity: 1;
}
// Internet Explorer 10+
&:-ms-input-placeholder {
color: $color;
}
// Safari and Chrome
&::-webkit-input-placeholder {
color: $color;
}

&:placeholder-shown {
text-overflow: ellipsis;
}
}
以上是我个人写的scss常用的函数,当然,肯定有很多没有写到,欢迎各位补充指正,若不明白如何调用的同志,欢迎留言。

原文链接:https://blog.csdn.net/liuxin00020/article/details/81355944

标签:scss,direction,函数,color,height,width,mixin,null
来源: https://www.cnblogs.com/onesea/p/15005474.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有