效果展示
页面结构组成
从上述的效果图可以看出,此页面的布局比较简单,采用常规的布局就可以实现
CSS / JavaScript 知识点
- backdrop-filter 属性回顾
- mousemove 事件
实现页面布局
<section>
<h2>Frosted Glass</h2>
<div class="glass"></div>
</section>
section {
position: relative;
display: flex;
justify-content: center;
align-items: center;
min-height: 100vh;
background: url(./bg.jpg) center;
background-attachment: fixed;
background-size: cover;
overflow: hidden;
}
section h2 {
font-size: 5em;
color: #fff;
pointer-events: none;
text-shadow: 0 10px 20px rgba(0, 0, 0, 0.5);
}
section .glass {
position: absolute;
pointer-events: none;
transform: translate(-50%, -50%);
width: 300px;
height: 200px;
box-shadow: 0 25px 50px rgba(0, 0, 0, 0.2);
background: transparent;
backdrop-filter: blur(10px);
transition: 0.5s;
}
实现毛玻璃跟随鼠标移动
毛玻璃跟随鼠标移动,我们可以使用mousemove
事件来完成功能实现。
document.addEventListener("mousemove", (e) => {
const glass = document.querySelector(".glass");
glass.style.left = e.offsetX + "px";
glass.style.top = e.offsetY + "px";
});
完整代码下载
完整代码下载