gpt4 book ai didi

html - 如何将这种 Font Awesome (v5.0) 颜色从白色转换为我的自定义颜色?

转载 作者:太空宇宙 更新时间:2023-11-04 07:32:04 28 4
gpt4 key购买 nike

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

最佳答案

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

但是,当您设置此 CSS 时 fill梯度值,对于引擎,现在是 <urlFunc>值,最终解析为 <image>值(value)*。所以从 <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 过滤。

关于html - 如何将这种 Font Awesome (v5.0) 颜色从白色转换为我的自定义颜色?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49290475/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com