【问题标题】:Create four color gradient mixin in bootstrap在引导程序中创建四色渐变混合
【发布时间】:2014-12-02 06:32:33
【问题描述】:

我只是想使用 bootstrap 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 文件中时,它对我有用。但我正在寻找创建渐变混合的解决方案。

这是我的渐变的纯 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 );

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

【问题讨论】:

标签: css twitter-bootstrap less linear-gradients


【解决方案1】:

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

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

#namespace > mixin();

> 在此调用中是可选的。

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

您应该按如下方式调用此 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 更多。输出取决于您编译引导程序的方式。默认构建过程生成上述输出。

当您使用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 的开发者版本,无需在生产代码中使用。

当然,您也可以编写自己的 mixin,其输出与纯 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

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

【讨论】:

  • 谢谢你的回答。但仍然没有运气。查看我的纯 CSS,这是我在编译渐变 mixin 后需要得到的。
  • 这正是我所需要的。非常感谢你。我以类似的方式尝试过,但无法弄清楚。你能告诉我我的代码有什么问题吗?
  • 我希望只有你的电话是错误的:#gradient.vertical-custom() 而不是#gradient > .vertical-custom()
  • 但在某些情况下,这种调用对我有用。例如:#gradient.vertical-three-colors(); (这是有效的)
  • 是的,确实,您对codepen.io/bassjobsen/pen/wBKrPK 的看法似乎是对的,它来自github.com/less/less.js/issues/1205
猜你喜欢
  • 2021-11-20
  • 1970-01-01
  • 1970-01-01
  • 2019-09-20
  • 2016-04-02
  • 1970-01-01
  • 2022-01-15
  • 2013-12-20
  • 1970-01-01
相关资源
最近更新 更多