css 实现 png 更改颜色

方案①

让ui 抠图, 把需要该颜色的地方 挖空, 
其余部分白色, 然后通过css 上色就好 [ 不怕ui拿刀砍你, 可以这么试试 ]

方案②

使用 css 中的  filter  属性

<div class="aaa">
   <div
      v-for="item in 10"
      :key="item"
      class="b"
      :style="`background:${getRgb()}`"
    ></div>
    <img src="../../assets/icon/cart_list.png">
</div>


.aaa {
  position: relative;
  .b {
    position: absolute;
    width: 50px;
    height: 50px;
    left: -100px;
    top: 50px;
    filter: drop-shadow(100px 0 0 red);
  }
}

 
安卓 和 window 上展示良好, 甚至很棒!
但是 ios 或者 mac 上, 就不忍直視了. 
ios 上只有当本体 在可视范围内 , 才能产生投影 

方案③

个人认为的, 最好方案

使用  mask-image 属性, 实现效果

 <div class="icon-box"> </div>


.icon-box {
  -webkit-mask-image: url("../../assets/icon/minu.png");
  mask-image: url("../../assets/icon/minu.png");
  width:50px;
  height:50px;
  background:${color};
  mask-size: contain;
  mask-position: center;
  mask-repeat: no-repeat;
  -webkit-mask-size: contain;
  -webkit-mask-position: center;
  -webkit-mask-repeat: no-repeat;
}

经过实测, 安卓 ok , ios ok , 十分完美


以下是代码

<template>
  <!-- -webkit-mask-image 兼容市面上大部分瀏覽器 -->
  <!-- mask-image 兼容火狐瀏覽器 -->
  <div
    class="icon-box"
    :style="`-webkit-mask-image: url(${src});mask-image: url(${src});width:${width};height:${height};background:${color};`"
  >
  </div>
</template>

<script>
export default {
  name: "self-icon",
  props: {
    // 圖片地址
    src: {
      default () {
        return '';
      }
    },
    // 容器寬
    width: {
      default () {
        return '14px';
      }
    },
    // 容器高
    height: {
      default () {
        return '14px';
      }
    },
    // 填充色
    color: {
      default () {
        return 'var(--themeColor)' // 默认主题色
      }
    }
  }
}
</script>

<style lang="less" scoped>
.icon-box {
  mask-size: contain;
  mask-position: center;
  mask-repeat: no-repeat;
  -webkit-mask-size: contain;
  -webkit-mask-position: center;
  -webkit-mask-repeat: no-repeat;
}
</style>

  • 0
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值