如何操作 - 图像叠加图标
学习如何在悬停时创建图像叠加图标效果。
图像叠加图标
将鼠标悬停在图像上即可看到叠加效果。

如何创建叠加图像图标
步骤 1) 添加 HTML
示例
<!-- 添加图标库 -->
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/4.7.0/css/font-awesome.min.css">
<div class="container">
<img src="img_avatar.png" alt="Avatar" class="image">
<div class="overlay">
<a href="#" class="icon" title="用户个人资料">
<i class="fa fa-user"></i>
</a>
</div>
</div>
步骤 2) 添加 CSS
示例
/* 容器需要定位叠加层。根据需要调整宽度 */
.container {
position: relative;
width: 100%;
max-width: 400px;
}
/* 使图像响应式 */
.image {
width: 100%;
height: auto;
}
/* 叠加效果(全高全宽)- 覆盖在容器和图像之上 */
.overlay {
position: absolute;
top: 0;
bottom: 0;
left: 0;
right: 0;
height: 100%;
width: 100%;
opacity: 0;
transition: .3s ease;
background-color: red;
}
/* 当您将鼠标移到容器上时,叠加图标会淡入 */
.container:hover .overlay {
opacity: 1;
}
/* 叠加层内的图标在垂直和水平方向上居中 */
.icon {
color: white;
font-size: 100px;
position: absolute;
top: 50%;
left: 50%;
transform: translate(-50%, -50%);
-ms-transform: translate(-50%, -50%);
text-align: center;
}
/* 当您将鼠标移到图标上时,更改颜色 */
.fa-user:hover {
color: #eee;
}
自己动手试一试 »
提示:另请参阅我们 如何操作 - 图像悬停叠加 中的其他图像叠加效果(淡入、滑动等)。
访问我们的 CSS 图像教程,了解更多关于如何设置图像样式的信息。