常用的scss函數(mixin)

scss自出來之後,廣受歡迎,如何能快速寫出想要的css呢,可自定義一些scss方法,本人羅列了一些最近用到的scss函數,其實包括文本超出範圍的格式化、彈性盒子居中、左浮動、右浮動、鼠標上移樣式改變等

1、文本超出範圍,顯示省略號

/*文本格式化,超出範圍,顯示省略號*/
@mixin text-overflow($width:100%,$display:block) {
  width: $width;
  display: $display;
  white-space: nowrap;
  -ms-text-overflow: ellipsis;
  text-overflow: ellipsis;
  overflow: hidden;
}

調用@include text-overflow();也可以自己傳入參數,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 flex-center($direction:row,$justify:center,$align:center,$flex-wrap: null) {
  display: -webkit-flex;
  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;
}

以上是我個人寫的scss常用的函數,當然,肯定有很多沒有寫到,歡迎各位補充指正,若不明白如何調用的同志,歡迎留言。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章