如何将这种字体真棒 (v5.0) 颜色从白色转换为我的自定义颜色?
How can I transition this font awesome (v5.0) color from white to my custom color?
我只是在做一个初学者项目,当我将鼠标悬停在 Instagram 图标上时,我希望它能慢慢过渡到我从 Whosebug 复制的一些 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>
浏览器开始支持 CSS 转换 <color>
值,因此他们接受在您的 CSS fill: <color>
和另一个 fill: <color>
之间进行转换].
但是,当你将这个CSS fill
值设置为梯度时,对于引擎来说,它现在是一个<urlFunc>
值,最终解析为 <image>
值*。因此无法从 <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 似乎不支持 CSS 在没有前缀的内部 SVG 节点上进行过滤.
我只是在做一个初学者项目,当我将鼠标悬停在 Instagram 图标上时,我希望它能慢慢过渡到我从 Whosebug 复制的一些 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>
浏览器开始支持 CSS 转换 <color>
值,因此他们接受在您的 CSS fill: <color>
和另一个 fill: <color>
之间进行转换].
但是,当你将这个CSS fill
值设置为梯度时,对于引擎来说,它现在是一个<urlFunc>
值,最终解析为 <image>
值*。因此无法从 <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 似乎不支持 CSS 在没有前缀的内部 SVG 节点上进行过滤.