简介
在本实验中,我们将学习如何使用 CSS 在悬停时创建图像叠加效果。这种效果为图像增添了时尚和专业的感觉,使其更具吸引力和互动性。通过使用伪元素和过渡,我们将创建一个平滑且视觉上吸引人的叠加层,从而提升任何网站的用户体验。
This tutorial is from open-source community. Access the source code
💡 本教程由 AI 辅助翻译自英文原版。如需查看原文,您可以 切换至英文原版
在本实验中,我们将学习如何使用 CSS 在悬停时创建图像叠加效果。这种效果为图像增添了时尚和专业的感觉,使其更具吸引力和互动性。通过使用伪元素和过渡,我们将创建一个平滑且视觉上吸引人的叠加层,从而提升任何网站的用户体验。
虚拟机中已经提供了 index.html
和 style.css
。
要在悬停时显示图像叠加效果,请执行以下步骤:
::before
和 ::after
伪元素。设置它们的 opacity
、transform
和 transition
以产生所需效果。<figcaption>
。设置 display: flex
、flex-direction: column
和 justify-content: center
以使文本在图像中居中。:hover
伪选择器来更新所有元素的 opacity
和 transform
并显示叠加层。以下是要使用的 HTML 代码:
<figure class="hover-img">
<img src="https://picsum.photos/id/200/440/320.jpg" />
<figcaption>
<h3>Lorem <br />Ipsum</h3>
</figcaption>
</figure>
以下是要使用的 CSS 代码:
.hover-img {
display: inline-block;
margin: 8px;
width: 100%;
max-width: 320px;
min-width: 240px;
overflow: hidden;
position: relative;
text-align: center;
background-color: #000;
color: #fff;
}
.hover-img * {
box-sizing: border-box;
transition: all 0.45s ease;
}
.hover-img::before,
.hover-img::after {
content: "";
position: absolute;
top: 0;
bottom: 0;
left: 0;
right: 0;
background-color: rgba(0, 0, 0, 0.5);
border-top: 32px solid rgba(0, 0, 0, 0.5);
border-bottom: 32px solid rgba(0, 0, 0, 0.5);
z-index: 1;
opacity: 0;
transform: scaleY(2);
transition: all 0.3s ease;
}
.hover-img::before {
content: "";
top: 0;
bottom: auto;
}
.hover-img img {
vertical-align: top;
max-width: 100%;
backface-visibility: hidden;
}
.hover-img figcaption {
position: absolute;
top: 0;
bottom: 0;
left: 0;
right: 0;
align-items: center;
display: flex;
flex-direction: column;
justify-content: center;
line-height: 1.1em;
opacity: 0;
z-index: 2;
transition-delay: 0.1s;
font-size: 24px;
font-family: sans-serif;
font-weight: 400;
letter-spacing: 1px;
text-transform: uppercase;
}
.hover-img:hover::before,
.hover-img:hover::after {
transform: scale(1);
opacity: 1;
}
.hover-img:hover img {
opacity: 0.7;
}
.hover-img:hover figcaption {
opacity: 1;
}
请点击右下角的“Go Live”以在端口 8080 上运行 Web 服务。然后,你可以刷新“Web 8080”标签页来预览网页。
恭喜你!你已经完成了“悬停时的图像叠加”实验。你可以在 LabEx 中练习更多实验来提升你的技能。