0

我只是在做一个初学者项目,当我将鼠标悬停在 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>

4

1 回答 1

1

浏览器开始支持<color>值的 CSS 转换,因此它们确实接受在您的 CSSfill: <color>到另一个fill: <color>.

但是,当您将此 CSSfill值设置为渐变时,对于引擎来说,它现在是一个<urlFunc>值,最终会解析为一个<image>值*。所以从 a 转换<color>到这个新值是不可能的。

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


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

.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);
}
<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>

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

于 2018-03-15T05:39:00.570 回答