mask 渐变遮罩属性

图片遮罩

  • hasBgBlock 盒子,背景图为 pic4,仅用于观察渐变过渡效果,可忽略
  • imgBox 盒子
    • 背景图为 pic1
    • 遮罩图层为 mask_front
    • mask-image 的 url 属性生效后,会按照 url 中图片的形状来遮罩背景图


html 复制代码
<div class="hasBgBlock">
  <div class="imgBox"></div>
</div>
<style>
  html,
  body {
    margin: 0;
    padding: 0;
  }

  .hasBgBlock {
    width: 100%;
    background: url("images/pic4.png") no-repeat;
    background-size: cover;
    padding: 80px 0;
  }

  .imgBox {
    width: 640px;
    height: 360px;
    margin: 0 auto;
    background: url("images/pic1.png") no-repeat;
    background-size: cover;
    
    /* 标准属性 */
    mask-image: url('images/mask_front.png');
    mask-size: contain;
    mask-repeat: no-repeat;
    mask-position: center;

    /* 旧版适配 */
    -webkit-mask-image: url('images/mask_front.png');
    -webkit-mask-size: contain;
    -webkit-mask-repeat: no-repeat;
    -webkit-mask-position: center;
  }
</style>

渐变效果

  • hasBgBlock 盒子,背景图为 pic4,仅用于观察渐变过渡效果,可忽略
  • imgBox 盒子
    • 背景图为 pic1
    • 遮罩图层设置为线性渐变从右到左,从透明到显示
    • 色号 #000,可以替换为其他任何非透明颜色
html 复制代码
<div class="hasBgBlock">
  <div class="imgBox"></div>
</div>
<style>
  html,
  body {
    margin: 0;
    padding: 0;
  }

  .hasBgBlock {
    width: 100%;
    background: url("images/pic4.png") no-repeat;
    background-size: cover;
    padding: 80px 0;
  }

  .imgBox {
    width: 640px;
    height: 360px;
    margin: 0 auto;
    background: url("images/pic1.png") no-repeat;
    background-size: cover;

    mask-image: linear-gradient(to right, transparent, #000);
    -webkit-mask-image: linear-gradient(to right, transparent, #000);
  }
</style>

demo

  • 线性渐变 demo
css 复制代码
mask-image: linear-gradient(to right, transparent, #e73d3d 10%, #68cf3f 90%, transparent);
-webkit-mask-image: linear-gradient(to right, transparent, #e73d3d 10%, #68cf3f 90%, transparent);
  • 径向渐变 demo
css 复制代码
mask-image: radial-gradient(circle at center, #fff 30%, transparent 70%);
mask-size: 100% 100%;
mask-repeat: no-repeat;
-webkit-mask-image: radial-gradient(circle at center, #fff 30%, transparent 70%);
-webkit-mask-size: 100% 100%;
-webkit-mask-repeat: no-repeat;
相关推荐
福兮说2 小时前
前端下载大文件,点了按钮半天没反应:fetch + blob 的三个问题和三种替代写法
前端·javascript·性能优化·文件下载
GreenTea3 小时前
我把 Agent 的 while 循环拆掉了:一种你可能没想到的 Agent 架构
前端·后端·架构
科技观察哨3 小时前
六足平台选型与纳米定位系统集成:HEB-640六自由度位移台在半导体光刻对准中的参数边界与国产替代评估
前端·人工智能
niucloud-admin3 小时前
JAVA V6 多商户商城 开发文档——web前端
前端
颜进强4 小时前
16 · NestJS LifecycleEvents 生命周期事件:五个钩子、三条边界,和 rag-server 的优雅停机
前端·后端·ai编程
今年下半年5 小时前
【VUE】整合腾讯地图、自定义区域边界、村委名称及资产统计(放大显示资产点位)
前端·javascript·vue.js
郑州光合科技余经理5 小时前
同城电商系统:库存变更怎么同步到订单
java·开发语言·前端·后端·uni-app·php·ai编程
前端snow7 小时前
ai agent --- DeepAgents 中间件
前端
data analyse 4567 小时前
报表能自定义的国内统计工具有哪些
前端·数据分析