在bootstrap中创建四种颜色渐变mixin

时间:2014-12-02 06:32:33

标签: css css3 twitter-bootstrap less linear-gradients

我只是想使用bootstrap mixin函数创建一个双色渐变。所以我的渐变应该是这样的:

enter image description here

当使用bootstrap gradient mixin时,我发现没有任何功能可以满足我的要求。所以我尝试制作自己的渐变混合并将其添加到bootstrap / less / mixins / grandients.less。但是我的职责没有完成我的工作..

这是我添加到gradients.less

的渐变mixin
  .vertical-custom(@start-color: #ed3537; @start-percent: 0%; @mid-color: #ed3537; @color-stop: 50%;  @mid-color-2: #fb3e40; @color-stop-2: 50%; @end-color: #fb3e40; @end-percent: 100%) {
    background-image: -webkit-linear-gradient(top, @start-color @start-percent, @mid-color, @color-stop, @mid-color-2, @color-stop-2, @end-color @end-percent);  // Safari 5.1-6, Chrome 10+
    background-image: -o-linear-gradient(top, @start-color @start-percent, @mid-color, @color-stop, @mid-color-2, @color-stop-2, @end-color @end-percent);  // Opera 12
    background-image: linear-gradient(to bottom, @start-color @start-percent, @mid-color, @color-stop, @mid-color-2, @color-stop-2, @end-color @end-percent); // Standard, IE10, Firefox 16+, Opera 12.10+, Safari 7+, Chrome 26+
    background-repeat: repeat-x;
    //filter: e(%("progid:DXImageTransform.Microsoft.gradient(startColorstr='%d', endColorstr='%d', GradientType=0)",argb(@start-color),argb(@end-color))); // IE9 and down
  }

我称之为

#gradient.vertical-custom(@start-color: #ed3537; @start-percent: 0%; @mid-color: #ed3537; @color-stop: 50%;  @mid-color-2: #fb3e40; @color-stop-2: 50%; @end-color: #fb3e40; @end-percent: 100%); 

当我直接将纯CSS添加到我的LESS文件时,它对我有用。但我正在寻找创建梯度mixin的解决方案。

这是我渐变的纯CSS:

background: #ed3537;
background: -moz-linear-gradient(top, #ed3537 0%, #ed3537 50%, #fb3e40 50%, #fb3e40 100%);
background: -webkit-gradient(linear, left top, left bottom, color-stop(0%,#ed3537), color-stop(50%,#ed3537), color-stop(50%,#fb3e40), color-stop(100%,#fb3e40));
background: -webkit-linear-gradient(top, #ed3537 0%,#ed3537 50%,#fb3e40 50%,#fb3e40 100%);
background: -o-linear-gradient(top, #ed3537 0%,#ed3537 50%,#fb3e40 50%,#fb3e40 100%);
background: -ms-linear-gradient(top, #ed3537 0%,#ed3537 50%,#fb3e40 50%,#fb3e40 100%);
background: linear-gradient(to bottom, #ed3537 0%,#ed3537 50%,#fb3e40 50%,#fb3e40 100%);
filter: progid:DXImageTransform.Microsoft.gradient( startColorstr='#ed3537', endColorstr='#fb3e40',GradientType=0 );

希望有人可以帮助我。 谢谢。

1 个答案:

答案 0 :(得分:2)

Bootstrap的渐变是命名空间(参见:http://lesscss.org/features/#features-overview-feature-namespaces-and-accessors),可以在less / mixins / gradients.less文件中找到。

应该按如下方式调用命名空间的mixin:

#namespace > mixin();

>在此次通话中是可选的。

Bootstrap在.vertical-three-colors() mixin命名空间中为您提供#gradient,与您的纯CSS最匹配。

您应该按如下方式调用此mixin(例如,在bootstrap.less文件的末尾):

div.gradient {
#gradient > .vertical-three-colors(#ed3537; #fb3e40; 50%; #fb3e40;);
}

前面的输出:

div.gradient {
  background-image: -webkit-linear-gradient(#ed3537, #fb3e40 50%, #fb3e40);
  background-image: -o-linear-gradient(#ed3537, #fb3e40 50%, #fb3e40);
  background-image: linear-gradient(#ed3537, #fb3e40 50%, #fb3e40);
  background-repeat: no-repeat;
  filter: progid:DXImageTransform.Microsoft.gradient(startColorstr='#ffed3537', endColorstr='#fffb3e40', GradientType=0);
}

演示:http://codepen.io/bassjobsen/pen/ogjeJE

请注意,您的纯CSS支持比Bootstrap的mixin更多的浏览器。 out取决于编译bootstrap的方式。默认构建过程会生成上述输出。

使用lessc --autoprefix="last 20 versions" grsdient.less编译以下Less代码时:

div.gradient {
  background-color: #ed3537;
  background-image: linear-gradient(#ed3537, #fb3e40 50%, #fb3e40);
  background-repeat: no-repeat;
  filter: progid:DXImageTransform.Microsoft.gradient(startColorstr='#ffed3537', endColorstr='#fffb3e40', GradientType=0);
}

将输出:

div.gradient {
  background-color: #ed3537;
  background-image: -webkit-gradient(linear, left top, left bottom, from(#ed3537), color-stop(50%, #fb3e40), to(#fb3e40));
  background-image: -webkit-linear-gradient(#ed3537, #fb3e40 50%, #fb3e40);
  background-image: -moz-linear-gradient(#ed3537, #fb3e40 50%, #fb3e40);
  background-image: -o-linear-gradient(#ed3537, #fb3e40 50%, #fb3e40);
  background-image: linear-gradient(#ed3537, #fb3e40 50%, #fb3e40);
  background-repeat: no-repeat;
  filter: progid:DXImageTransform.Microsoft.gradient(startColorstr='#ffed3537', endColorstr='#fffb3e40', GradientType=0);
}

-ms-linear-gradient前缀仅针对IE10的开发人员版本,无需在生产代码中使用它。

当然你也可以编写自己的mixins,输出与纯CSS完全相同:

.vertical-custom(@start-color: #ed3537; @start-percent: 0%; @mid-color: #ed3537; @color-stop: 50%;  @mid-color-2: #fb3e40; @color-stop-2: 50%; @end-color: #fb3e40; @end-percent: 100%)
{
background: @start-color;
background: -moz-linear-gradient(top, @start-color, @mid-color @color-stop,  @mid-color-2 @color-stop-2, @end-color @end-percent);
background: -webkit-gradient(linear, left top, left bottom, color-stop(@start-percent,@start-color), color-stop(@color-stop,@mid-color), color-stop(@color-stop-2,@mid-color-2), color-stop(@end-percent,@end-color));
background: -webkit-linear-gradient(top, @start-color @start-percent,@mid-color @color-stop,@mid-color-2 @color-stop-2,@end-color @end-percent);
background: -o-linear-gradient(top, @start-color @start-percent,@mid-color @color-stop,@mid-color-2 @color-stop-2,@end-color @end-percent);
background: -ms-linear-gradient(top, @start-color @start-percent,@mid-color @color-stop,@mid-color-2 @color-stop-2,@end-color @end-percent);
background: linear-gradient(to bottom, @start-color @start-percent,@mid-color @color-stop,@mid-color-2 @color-stop-2,@end-color @end-percent);
filter: e(%("progid:DXImageTransform.Microsoft.gradient(startColorstr='%d', endColorstr='%d', GradientType=1)",argb(@start-color),argb(@end-color)));
}

现在出现以下代码:

div.gradient {
.vertical-custom();
}

输出:

div.gradient {
  background: #ed3537;
  background: -moz-linear-gradient(top, #ed3537, #ed3537 50%, #fb3e40 50%, #fb3e40 100%);
  background: -webkit-gradient(linear, left top, left bottom, color-stop(0%, #ed3537), color-stop(50%, #ed3537), color-stop(50%, #fb3e40), color-stop(100%, #fb3e40));
  background: -webkit-linear-gradient(top, #ed3537 0%, #ed3537 50%, #fb3e40 50%, #fb3e40 100%);
  background: -o-linear-gradient(top, #ed3537 0%, #ed3537 50%, #fb3e40 50%, #fb3e40 100%);
  background: -ms-linear-gradient(top, #ed3537 0%, #ed3537 50%, #fb3e40 50%, #fb3e40 100%);
  background: linear-gradient(to bottom, #ed3537 0%, #ed3537 50%, #fb3e40 50%, #fb3e40 100%);
  filter: progid:DXImageTransform.Microsoft.gradient(startColorstr='#ffed3537', endColorstr='#fffb3e40', GradientType=1);
}

演示:http://codepen.io/bassjobsen/pen/LEpzEm

确保在使用.vertical-custom

调用时,#gradient mixin已添加到gradients.less文件中的#gradient > .vertical-custom();命名空间内