Sass - 将Hex转换为RGBa以获得背景不透明度

Ric*_*hoe 190 css sass mixins background-color rgba

我有以下Sass mixin,它是RGBa示例的一半完整修改:

@mixin background-opacity($color, $opacity: .3) {
    background: rgb(200, 54, 54); /* The Fallback */
    background: rgba(200, 54, 54, $opacity);
} 
Run Code Online (Sandbox Code Playgroud)

我申请$opacity好了,但现在我被困在那个$color部分.我将发送到mixin的颜色将是HEX而不是RGB.

我的例子用途是:

element {
    @include background-opacity(#333, .5);
}
Run Code Online (Sandbox Code Playgroud)

如何在此mixin中使用HEX值?

hop*_*per 381

RGBA()函数可以接受单个十六进制颜色以及小数RGB值.例如,这可以正常工作:

@mixin background-opacity($color, $opacity: 0.3) {
    background: $color; /* The Fallback */
    background: rgba($color, $opacity);
}

element {
     @include background-opacity(#333, 0.5);
}
Run Code Online (Sandbox Code Playgroud)

但是,如果您需要将十六进制颜色分解为RGB组件,则可以使用red(),green()blue()函数来执行此操作:

$red: red($color);
$green: green($color);
$blue: blue($color);

background: rgb($red, $green, $blue); /* same as using "background: $color" */
Run Code Online (Sandbox Code Playgroud)

  • 据我所知,RGBA增加*不透明度*,意味着你可以看到它背后的元素.使用标准的`hex`,你不能这样做. (2认同)
  • 最好像使用 `rgba(#000000, 0.6)` 那样使用 `rgba($color, $alpha)`,因为结果是一样的,不需要重新发明轮子。;) (2认同)

use*_*047 106

有一个内置的mixin: transparentize($color, $amount);

background-color: transparentize(#F05353, .3);
Run Code Online (Sandbox Code Playgroud)

金额应在0到1之间;

官方Sass文档(模块:Sass :: Script :: Functions)

  • 作为旁注:$ amount是减去多少,而不是你想要设置的值 (14认同)
  • 工作得很好,除了金额似乎是反向意义,你想让它'90%`透明,以获得结果`.1` (3认同)

Reg*_*ham 29

SASS有一个内置的rgba()函数来评估值.

rgba($color, $alpha)
Run Code Online (Sandbox Code Playgroud)

例如

rgba(#00aaff, 0.5) => rgba(0, 170, 255, 0.5)
Run Code Online (Sandbox Code Playgroud)

使用您自己的变量的示例:

$my-color: #00aaff;
$my-opacity: 0.5;

.my-element {
  color: rgba($my-color, $my-opacity);
}
Run Code Online (Sandbox Code Playgroud)

输出:

.my-element {
  color: rgba(0, 170, 255, 0.5);
}
Run Code Online (Sandbox Code Playgroud)


m.E*_*afi 6

你可以尝试这个解决方案,是最好的... url(github)

// Transparent Background
// From: http://stackoverflow.com/questions/6902944/sass-mixin-for-background-transparency-back-to-ie8

// Extend this class to save bytes
.transparent-background {
  background-color: transparent;
  zoom: 1;
}

// The mixin
@mixin transparent($color, $alpha) {
  $rgba: rgba($color, $alpha);
  $ie-hex-str: ie-hex-str($rgba);
  @extend .transparent-background;
  background-color: $rgba;
  filter:progid:DXImageTransform.Microsoft.gradient(startColorstr=#{$ie-hex-str},endColorstr=#{$ie-hex-str});
}

// Loop through opacities from 90 to 10 on an alpha scale
@mixin transparent-shades($name, $color) {
  @each $alpha in 90, 80, 70, 60, 50, 40, 30, 20, 10 {
    .#{$name}-#{$alpha} {
      @include transparent($color, $alpha / 100);
    }
  }
}

// Generate semi-transparent backgrounds for the colors we want
@include transparent-shades('dark', #000000);
@include transparent-shades('light', #ffffff);
Run Code Online (Sandbox Code Playgroud)

  • 你的答案很好,但似乎不必要的复杂.我猜透明的阴影与我的问题无关,虽然你能解释一下透明背景类是什么吗?如果我不使用透明色调混合,我想我不需要它? (5认同)