如何将此字体真棒(v5.0)颜色从白色转换为自定义颜色?

时间:2018-03-15 02:10:25

标签: html css font-awesome-5

我正在开发一个初学者项目,当我将鼠标悬停在Instagram图标上时,我希望它能够从我从StackOverflow复制的一些svg代码中慢慢过渡到渐变色。我理解SVG代码是如何工作的(通常),但无法弄清楚如何使转换工作。

这是一段必要的代码,以及我迄今为止尝试过的CSS。

我认为fill的结束转换价值是造成问题的原因。如果我将其更改为其他值,例如"blue""red",则转换效果正常。



.social-container {
	position: absolute;
	bottom: 0;
	color: white;
	border: 1px solid white;
	display: flex;
	width: 150px;
	height: 35px;
	justify-content: space-between;
	align-items: center;
}


svg {
	transition: fill 3s ease;
}

.instagram-logo svg * {
  transition: all 2s ease;
	fill: black;
}

.instagram-logo:hover svg * {
	fill: url(#rg);
}

/* So I noticed the issue is the .instagram-logo:hover svg * {}. When I use a solid color like "blue" for the fill value, the transition works flawlessly. But when using the url, it doesn't work at all */

<script src="https://use.fontawesome.com/releases/v5.0.8/js/all.js"></script>

<div class="instagram-logo">
  <svg width="0" height="0">
    <radialGradient id="rg" r="150%" cx="30%" cy="107%">
      <stop stop-color="#fdf497" offset="0" />
      <stop stop-color="#fdf497" offset="0.05" />
      <stop stop-color="#fd5949" offset="0.45" />
      <stop stop-color="#d6249f" offset="0.6" />
      <stop stop-color="#285AEB" offset="0.9" />
    </radialGradient>
  </svg>
  <i class="fab fa-instagram fa-2x"></i>
</div>
&#13;
&#13;
&#13;

1 个答案:

答案 0 :(得分:1)

浏览器开始支持<color>值的CSS转换,因此他们接受在您的CSS fill: <color>与其他fill: <color>之间进行转换。

但是,当您将此fill值设置为渐变时,对于引擎,它现在是<urlFunc>值,最终解析为<image>值*。因此,无法从<color>过渡到此新值。

* 虽然我不确定这个断言......

一种解决方案是设置CSS <filter>值的动画:

&#13;
&#13;
.instagram-logo{
  padding: 2px 8px;
}
.instagram-logo svg.fa-instagram path{
  fill: url(#rg);
}

/* from black to color */
.instagram-logo svg.fa-instagram{
  filter: brightness(0%) grayscale(100%);
  transition: filter 2s ease, -webkit-filter 2s ease;
}
.instagram-logo svg.fa-instagram:hover{
  filter: brightness(100%) grayscale(0%);
}

/* from white to color */
.instagram-logo.white{
  background: black;
}
.instagram-logo.white svg.fa-instagram{
  filter: brightness(0%) grayscale(100%) invert(1);
}
.instagram-logo.white svg.fa-instagram:hover{
  /*
    we need to make a new declaration
    because transitions can only be done on func values 
    so we need the same <filterFunc> at both hands of the transition
 */
  filter: brightness(100%) grayscale(0%) invert(0);
}
&#13;
<script src="https://use.fontawesome.com/releases/v5.0.8/js/all.js"></script>

<div class="instagram-logo">
  <svg class="hidden" width="0" height="0" style="position:absolute; z-index:-1">
    <radialGradient id="rg" r="150%" cx="30%" cy="107%">
      <stop stop-color="#fdf497" offset="0" />
      <stop stop-color="#fdf497" offset="0.05" />
      <stop stop-color="#fd5949" offset="0.45" />
      <stop stop-color="#d6249f" offset="0.6" />
      <stop stop-color="#285AEB" offset="0.9" />
    </radialGradient>
  </svg>
  <i class="fab fa-instagram fa-2x"></i>

</div>
<div class="instagram-logo white">
  <i class="fab fa-instagram fa-2x"></i>
</div>
&#13;
&#13;
&#13;

请注意,在给定的示例中,我在父<svg>节点上设置了过滤器,因为chrome似乎不支持在没有前缀的内部SVG节点上进行CSS过滤。