CSS/蒙层:修订间差异

来自康健生活
< CSS
Kangkk​(留言 | 贡献)
Kangkk​(留言 | 贡献)
无编辑摘要
 
(未显示同一用户的7个中间版本)
第27行: 第27行:


{{Warning|当遮罩层被设置 <code>pointer-events:none</code> 时不能有效监听 <code>mosuemove</code>,所以监听 <code>mosuemove</code> 事件需要在遮罩层的上一层 DOM 进行。}}
{{Warning|当遮罩层被设置 <code>pointer-events:none</code> 时不能有效监听 <code>mosuemove</code>,所以监听 <code>mosuemove</code> 事件需要在遮罩层的上一层 DOM 进行。}}
另外,对于 <code>outline</code>、<code>box-shadow</code> 等属于 <code>css</code> 的视觉效果,所以可以直接穿透事件。


== 遮罩层 ==
== 遮罩层 ==
第42行: 第44行:
=== 截图模拟实现 ===
=== 截图模拟实现 ===
截取一张镂空位置的图片,创建遮罩层,将图片定位在遮罩层上面。
截取一张镂空位置的图片,创建遮罩层,将图片定位在遮罩层上面。
<html><iframe width="100%" height="300" src="//www.kangjian.life/date/2020/截图模拟实现镂空/iframe.html" frameborder="0"></iframe></html>
<html><iframe width="100%" height="300" src="//kangkk.com/htdocs/date/2020/截图模拟实现镂空/iframe.html" frameborder="0"></iframe></html>


缺点:
缺点:
第52行: 第54行:
用多个 div 拼接遮罩层。
用多个 div 拼接遮罩层。


<html><iframe width="100%" height="300" src="//www.kangjian.life/date/2020/div拼接镂空方法/iframe.html" frameborder="0"></iframe></html>
<html><iframe width="100%" height="300" src="//kangkk.com/htdocs/date/2020/div拼接镂空方法/iframe.html" frameborder="0"></iframe></html>


缺点:
缺点:
第65行: 第67行:
width="100%"
width="100%"
height="300"
height="300"
src="//www.kangjian.life/date/2020/border边距镂空方法/iframe.html"
src="//kangkk.com/htdocs/date/2020/border边距镂空方法/iframe.html"
frameborder="0"></iframe></html>
frameborder="0"></iframe></html>


第79行: 第81行:
width="100%"
width="100%"
height="300"
height="300"
src="//www.kangjian.life/date/2020/border镂空穿透事件方法/iframe.html"
src="//kangkk.com/htdocs/date/2020/border镂空穿透事件方法/iframe.html"
frameborder="0"></iframe></html>
frameborder="0"></iframe></html>


第87行: 第89行:
width="100%"
width="100%"
height="300"
height="300"
src="//www.kangjian.life/date/2020/outline属性镂空方法/iframe.html"
src="//kangkk.com/htdocs/date/2020/outline属性镂空方法/iframe.html"
frameborder="0"></iframe></html>
frameborder="0"></iframe></html>


第95行: 第97行:
width="100%"
width="100%"
height="300"
height="300"
src="//www.kangjian.life/date/2020/outline镂空穿透事件方法/iframe.html"
src="//kangkk.com/htdocs/date/2020/outline镂空穿透事件方法/iframe.html"
frameborder="0"></iframe></html>
frameborder="0"></iframe></html>


第103行: 第105行:
width="100%"
width="100%"
height="300"
height="300"
src="//www.kangjian.life/date/2020/box-shadow属性镂空方法/iframe.html"
src="//kangkk.com/htdocs/date/2020/box-shadow属性镂空方法/iframe.html"
frameborder="0"></iframe></html>
frameborder="0"></iframe></html>


第113行: 第115行:
=== mix-blend-mode属性镂空法 ===
=== mix-blend-mode属性镂空法 ===


<html><iframe width="100%" height="300" src="//www.kangjian.life/date/2020/mix-blend-mode属性镂空方法/iframe.html" frameborder="0"></iframe></html>
<html><iframe width="100%" height="300" src="//kangkk.com/htdocs/date/2020/mix-blend-mode属性镂空方法/iframe.html" frameborder="0"></iframe></html>


==== 事件穿透 ====
==== 事件穿透 ====


<html><iframe width="100%" height="300" src="//www.kangjian.life/date/2020/mix-blend-mode镂空法事件穿透/iframe.html" frameborder="0"></iframe></html>
<html><iframe width="100%" height="300" src="//kangkk.com/htdocs/date/2020/mix-blend-mode镂空法事件穿透/iframe.html" frameborder="0"></iframe></html>


=== canvas镂空方法 ===
=== canvas镂空方法 ===


<html><iframe width="100%" height="300" src="//www.kangjian.life/date/2020/canvas镂空方法/iframe.html" frameborder="0"></iframe></html>
<html><iframe width="100%" height="300" src="//kangkk.com/htdocs/date/2020/canvas镂空方法/iframe.html" frameborder="0"></iframe></html>


=== svg镂空方法 ===
=== svg镂空方法 ===


<html><iframe width="100%" height="300" src="//www.kangjian.life/date/2020/svg镂空方法/iframe.html" frameborder="0"></iframe></html>
<html><iframe width="100%" height="300" src="//kangkk.com/htdocs/date/2020/svg镂空方法/iframe.html" frameborder="0"></iframe></html>
== 参考 ==
* [https://www.zhangxinxu.com/study/201307/color-insert-background-image.html 张鑫旭:背景色镂空技术之color属性实现颜色可控图标实现实例页面]
* [https://developer.mozilla.org/en-US/docs/Web/CSS/mask-composite#Example mask-composite]

2025年8月30日 (六) 19:24的最新版本

引言

场景

  • 蒙层登陆效果

模糊效果(虚化)

filter:blur(15px); 示例
Css 蒙层 2
Css 蒙层 2
backdrop-filter: blur(15px); 示例
Css 蒙层 1
Css 蒙层 1
background-clip: border-box;
background: -webkit-linear-gradient(#eee, #333);
-webkit-background-clip: text;
-webkit-text-fill-color: transparent;
示例
mask-image
linear-gradient
background: linear-gradient(to top,rgba(0,0,0,.75), rgba(0,0,0,0))

相关属性

pointer-events

鼠标事件穿透。

在镂空事件穿透场景下,要在遮罩层阻止事件穿透并在镂空区域允许事件穿透,所以需要注意的是不能直接对遮罩层设置 pointer-events:none 属性,而是通过 JavaScript 监听鼠标移动,在镂空区域时设置 pointer-events:none,离开镂空区域设置 pointer-events:auto。

Warning: 当遮罩层被设置 pointer-events:none 时不能有效监听 mosuemove,所以监听 mosuemove 事件需要在遮罩层的上一层 DOM 进行。

另外,对于 outline、box-shadow 等属于 css 的视觉效果,所以可以直接穿透事件。

遮罩层

background: #fff;
position: fixed;
left: 0px;
top: 0px;
width: 100%;
height: 100%;
z-index: 10;
filter: alpha(opacity=60);
opacity: 0.6 !important;

镂空

截图模拟实现

截取一张镂空位置的图片,创建遮罩层,将图片定位在遮罩层上面。

缺点:

  • 适合静态不滚动的页面。

优点:

  • 兼容 IE6 以上。

div拼接镂空法

用多个 div 拼接遮罩层。

缺点:

  • 只能设计矩形遮罩层。

优点:

  • 可以穿透事件
  • 镂空固定

border边距镂空法

使用 border 镂空

缺点:

  • 遮罩层固定
  • 矩形镂空

优点:

  • 理论兼容 IE7 以上

事件穿透

outline属性镂空法

事件穿透

box-shadow属性镂空法

缺点:

  • 兼容 IE9

优点:

  • 设置 border-radius 属性可以圆形镂空

mix-blend-mode属性镂空法

事件穿透

canvas镂空方法

svg镂空方法

参考