scss Sass功能和mixin用于设置对比背景和前景色

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了scss Sass功能和mixin用于设置对比背景和前景色相关的知识,希望对你有一定的参考价值。

@import "contrast_mixin";

body {
  padding: 20px;
  font: bold 16px/24px Helvetica, Arial, sans-serif;
}

div {
  padding: 10px;
  margin-bottom: 10px;
  &.a { @include contrasted(#ef4444); }
  &.b { @include contrasted(#faa31b); }
  &.c { @include contrasted(#fff000); }
  &.d { @include contrasted(#82c341); }
  &.e { @include contrasted(#009f75); }
  &.f { @include contrasted(#88c6ed); }
  &.g { @include contrasted(#394ba0); }
  &.h { @include contrasted(#d54799); }
}
module Sass::Script::Functions
  
  def get_contrast_yiq(color, dark = Sass::Script::Color.new([0,0,0]), light = Sass::Script::Color.new([255,255,255]))
    yiq = ( (color.red*299) + (color.green*587) + (color.blue*114) ) / 1000;
    yiq >= 128 ? dark : light
  end
    
end
$contrasted-default-dark:   #000;
$contrasted-default-light:  #fff;

@mixin contrasted($bg, $dark:$contrasted-default-dark, $light:$contrasted-default-light){
  background-color: $bg;
  color: get_contrast_yiq($bg, $dark, $light);
}

以上是关于scss Sass功能和mixin用于设置对比背景和前景色的主要内容,如果未能解决你的问题,请参考以下文章

scss Sass mixin用于设置默认rem字体大小

scss 设置功能mixins sass

scss Sass - Mixins:SVG背景图片

scss 用于REM和PX的SASS Mixin

scss 用于媒体查询的Sass mixins

scss Sass mixin用于使表格响应