【问题标题】:How to overwrite linear-gradient function in Compass?如何在 Compass 中覆盖线性梯度函数?
【发布时间】:2012-11-25 20:49:57
【问题描述】:

我想使用代码here 覆盖Compass 附带的linear-gradient 函数。我该怎么做?

我认为我需要一种方法来导入linear-gradient 函数,然后在本地将其重命名为其他名称,以便我自己的linear-gradient 函数可以调用它。例如。类似:

@import "compass/css3/images";

// somehow make `original-lg` alias the current `linear-gradient`

@function linear-gradient($args...) {
  @return original-lg($args...) + ", " + fixed-lg-from-link-above($args...);
}

【问题讨论】:

    标签: compass-sass sass


    【解决方案1】:

    您尝试的操作将不起作用,因为我们正在处理必须拆分为不同属性的前缀值。作为链接代码的作者,这是我推荐使用它的方式。您将需要这些功能:

    @function convert-gradient-angle(
      $deg
    ) {
      @if type-of($deg) == 'number' {
        @return mod(abs($deg - 450), 360deg);
      } @else {
        $direction: compact();
        @if nth($deg,1) == 'to' {
          @if length($deg) < 2 {
            $direction: top;
            @warn "no direction given for 'to'. Using 'to bottom' as default.";
          } @else { $direction: opposite-position(nth($deg,2)); }
          @if length($deg) > 2 { $direction: append($direction, opposite-position(nth($deg,3)), space);}
        } @else {
          $direction: append($direction, to, space);
          @each $pos in $deg { $direction: append($direction, opposite-position($pos), space); }
        }
        @return $direction;
      }
    }
    
    @function convert-gradient(
      $angle,
      $details...
    ) {
      @return linear-gradient(convert-gradient-angle($angle), $details...);
    }
    

    问题是,如果您使用多背景或类似的东西,您将不得不自己在不同的属性中重复这些功能。如果您只想要一个简单的背景图像渐变,您可以使用它来简化:

    @mixin gradient-background-image(
      $gradient...
    ) {
      @include background-image(convert-gradient($gradient...));
      background-image: linear-gradient($gradient...);
    }
    

    否则,您将需要手动编写这两行,并根据需要添加其他层。

    【讨论】:

    • 是的,我希望得到类似于内置linear-gradient 函数的东西,这样我就可以做@include background-image(linear-gradient(...))。由于原始函数不知何故被转换为多个属性,我希望这个也能。
    • 是的,我也想要。它应该很快会在下一个 Compass 0.13 预发行版中推出,但它需要重新编写 Ruby 函数。不能简单地在 Sass 中完成。
    • 啊,“不能在 SASS 中完成”才是真正的答案。非常感谢。
    • 您没有回答原始问题。在我的情况下,我想覆盖 sass 预定义的颜色函数,如 lighten - 我想覆盖它们并在某些时候调用原始颜色函数
    • @cancerbero 你的问题听起来很有趣,而且它确实与这里的标题相匹配——但细节非常不同。我建议打开一个新问题。如果您愿意,请告诉我,我很乐意提供帮助 - 但我需要更多详细信息。
    猜你喜欢
    • 1970-01-01
    • 2018-03-20
    • 2017-12-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-03-06
    • 1970-01-01
    相关资源
    最近更新 更多