css3 filter 滤镜高斯模糊毛玻璃雾化弹窗效果

css3 的 filter 非常强大,在实际开发中也能提升逼格,比如 filter: blur 可以实现更优雅的弹窗遮罩。

效果:example.html

源码:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
<!DOCTYPE html>
<html>

<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>css3 filter 滤镜高斯模糊毛玻璃雾化弹窗效果</title>
<style>
.filter > * {
filter: blur(2px);
}
.filter > .mask {
filter: none;
}
.mask {
position: fixed;
top: 0;
left: 0;
bottom: 0;
right: 0;
background: rgba(0,0,0,0.35);
z-index: 999;
justify-content: center;
align-items: center;
display: none;
}
.mask.on {
display: flex;
}
.mask .container {
width: 300px;
height: 200px;
background: #fff;
}
</style>
</head>

<p>一些文本</p>

<button id="js_modal_nomal"> 常规弹窗点我 </button>

<button id="js_modal_blur"> 雾化弹窗点我 </button>

<body>
<script>
(function () {
var html = document.createElement('div');;
html.setAttribute('class', 'mask');
html.innerHTML = '<div class="container">点击任意位置关闭弹窗</div>';
document.body.appendChild(html);
function modal (type) {
if (type === 1) {
document.body.classList.add('filter');
}
html.classList.add('on');
};
html.addEventListener('click', function () {
html.classList.remove('on');
document.body.classList.remove('filter');
});
document.getElementById('js_modal_nomal').addEventListener('click', function () {
modal();
});
document.getElementById('js_modal_blur').addEventListener('click', function () {
modal(1);
});
})();
</script>
</body>

</html>

兼容性:

https://caniuse.com/#search=filter%20blur

css3 还有其他很多滤镜,本文不再一一举例,有兴趣的可以参考: https://developer.mozilla.org/zh-CN/docs/Web/CSS/filter

本文由 linx(544819896@qq.com) 创作,采用 CC BY 4.0 CN协议 进行许可。 可自由转载、引用,但需署名作者且注明文章出处。本文链接为: https://blog.jijian.link/2020-03-28/css3-filter-blur/

如果您觉得文章不错,可以点击文章中的广告支持一下!