5

我有一个 SVG,我想在某些事件中将它的颜色更改为红色,但是你不能用 SVG 作为背景图像来做到这一点,所以你必须使用 CSS image-mask。我正在使用 PHP 将我的 CSS 回显到 div 的样式属性上:

$jfid = "background-color:red;
        -webkit-mask-image:url(../like_icons/" . $iconfgg . ".svg);
         mask-image:url(../like_icons/" . $iconfgg . ".svg)"; 

喜欢

.buttonlikee {
    background: transparent;
    outline: none;
    border: none;
    margin-left: 10px;
    transition: 0.8s all ease
}
.ts{
  width: 34px;
  height: 32px;
  background-color:red;
  -webkit-mask-image:url(https://svgshare.com/i/CB7.svg);
  mask-image:url(https://svgshare.com/i/CB7.svg) 
}
<button class="buttonlikee">
  <div class="ts"></div>
</button>

这按预期工作,但返回相同 SVG 的重复图像。所以解决方案是no-repeat最后添加这样的:

$jfid = "background-color:red;
         -webkit-mask-image:url(../like_icons/" . $iconfgg . ".svg) no-repeat;
         mask-image:url(../like_icons/" . $iconfgg . ".svg) no-repeat"; 

作为回报,这给了我一个充满红色的 div,你看不到像这样的图标

.buttonlikee {
    background: transparent;
    outline: none;
    border: none;
    margin-left: 10px;
    transition: 0.8s all ease
}
.ts{
  width: 34px;
  height: 32px;
  background-color:red;
  -webkit-mask-image:url(https://svgshare.com/i/CB7.svg) no-repeat;
  mask-image:url(https://svgshare.com/i/CB7.svg) no-repeat
}
<button class="buttonlikee">
  <div class="ts"></div>
</button>

这是一个错误吗?有什么解决办法?

4

1 回答 1

7

no-repeat不是文档mask-image中看到的属性的有效命令。相反,您应该像这样使用属性mask-repeat

.buttonlikee {
    background: transparent;
    outline: none;
    border: none;
    margin-left: 10px;
    transition: 0.8s all ease
}
.ts {
  width: 34px;
  height: 32px;
  background-color:red;
  -webkit-mask-image: url(https://svgshare.com/i/CB7.svg);
  mask-image: url(https://svgshare.com/i/CB7.svg);
  -webkit-mask-repeat: no-repeat;
  mask-repeat: no-repeat;
}
<button class="buttonlikee">
  <div class="ts"></div>
</button>

否则,您可以使用mask属性简写:

.buttonlikee {
    background: transparent;
    outline: none;
    border: none;
    margin-left: 10px;
    transition: 0.8s all ease
}
.ts {
  width: 34px;
  height: 32px;
  background-color:red;
  -webkit-mask: url(https://svgshare.com/i/CB7.svg) no-repeat;
  mask: url(https://svgshare.com/i/CB7.svg) no-repeat;
}
<button class="buttonlikee">
  <div class="ts"></div>
</button>

于 2019-04-04T14:58:22.837 回答