简体   繁体   English

Sass / Compass-将变量传递给嵌套的背景图像混合

[英]Sass/Compass - passing a variable to a nested background-image mixin

I'm trying to pass a variable (a color hex-code) to the Compass background-image mixin that I've nested inside a mixin I've declared. 我正在尝试将变量(彩色十六进制代码)传递给嵌套在已声明的mixin中的Compass背景图像mixin。

When Compass tries to compile the CSS it throws the following error. 当Compass尝试编译CSS时,它将引发以下错误。

error sass/styles.scss (Line 103 of sass/_mixins.scss: Expected a color. Got: #fef1d0)

When I replace the variable with a hardcoded hex value ie #FEF1D0 in the background-image mixin the CSS is compiled without errors. 当我用硬编码的十六进制值(即,背景图像混合中的#FEF1D0替换变量时,CSS会正确编译。

Below is the code. 下面是代码。

// The variables
  // primary
  $yellow:           #FCB813;
  $blue:             #005696;

  // secondary
  $yellow-soft:       #FEF1D0;
  $blue-soft:         #D9E6EF;

// The mixin
  @mixin main-menu($primary, $secondary) {
      border-bottom: {
        color: $primary;
      style: solid;
    }
    background: #fff; // older browsers.
    @include background-image(linear-gradient(top, white 50%, $secondary 50%));
    background-size: 100% 200%;
    background-position: top;
    margin-left:10px;
    @include transition(all 0.5s ease);
    &:hover {
      background-position: bottom;
    }
  }

//Using the mixin
  #main-menu {
    $sections: (
      yellow $yellow $yellow-soft,
      blue $blue $blue-soft
      );
    @each $color in $sections {
      a.#{nth($color, 1)} {
        @include main-menu(#{nth($color, 2)}, #{nth($color, 3)});
      }
    }

The compiled CSS when $secondary is replaced with #FEF1D0 in the background-image mixin. $secondary在背景图像mixin中用#FEF1D0替换时的已编译CSS。 ie @include background-image(linear-gradient(top, white 50%, #FEF1D0 50%)); @include background-image(linear-gradient(top, white 50%, #FEF1D0 50%));

#main-menu a.yellow {
  border-bottom-color: #fcb813;
  border-bottom-width: 3px;
  border-bottom-style: solid;
  background: #fff;
  background-image: -webkit-gradient(linear, 50% 0%, 50% 100%, color-stop(50%, #ffffff), color-stop(50%, #fef1d0));
  background-image: -webkit-linear-gradient(top, #ffffff 50%, #fef1d0 50%);
  background-image: -moz-linear-gradient(top, #ffffff 50%, #fef1d0 50%);
  background-image: -o-linear-gradient(top, #ffffff 50%, #fef1d0 50%);
  background-image: linear-gradient(top, #ffffff 50%, #fef1d0 50%);
  background-size: 100% 200%;
  background-position: top;
  margin-left: 10px;
  -webkit-transition: all 0.5s ease;
  -moz-transition: all 0.5s ease;
  -o-transition: all 0.5s ease;
  transition: all 0.5s ease;
}

The goal is to have a background transition on hover state that fills the link background with a sliding transition of the bg-color from bottom to top, thanks to this great suggestion . 我们的目标是有一个充满友情链接后台与BG-颜色从底部到顶部,感谢这个伟大的滑动过渡悬停状态的背景转换的建议 Which works very nicely except for the way compass is parsing the variable. 除了指南针解析变量的方式之外,该方法非常有效。

The problem is in the @include parameters. 问题出在@include参数中。 You are using Sass interpolation for both arguments, it causes the mixin treat these variables as strings instead of, in this case, colors: 您在两个参数上都使用了Sass插值,这会导致mixin将这些变量视为字符串,而不是颜色(在这种情况下,将其视为颜色):

type_of(#FEF1D0); // returns color
type_of(#{#FEF1D0}); // returns string

You can pass a string to color property but linear-gradient is a function and it requires a color. 您可以将字符串传递给color属性,但是linear-gradient是一个函数,它需要一种颜色。

To solve this problem you should remove the interpolation of the second argument to pass it as a color. 要解决此问题,您应该删除第二个参数的插值,以将其作为颜色传递。 You can use the interpolation for the first argument but is unnecesary so I recommend you remove it. 您可以对第一个参数使用插值,但是不需要插值,因此建议您删除它。

So you should use: 因此,您应该使用:

@include main-menu(nth($color, 2), nth($color, 3));

instead of: 代替:

@include main-menu(#{nth($color, 2)}, #{nth($color, 3)})

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM