如何创建:图像叠加缩放

学习如何在悬停时创建图像叠加缩放效果。

图像叠加全屏缩放

请将鼠标悬停在图像上,即可查看缩放效果。

Avatar
Hello World

亲自试一试

如何创建叠加缩放效果

第一步 - 添加 HTML:

<div class="container">
  <img src="img_avatar.png" alt="Avatar" class="image">
  <div class="overlay">
    <div class="text">Hello World</div>
  </div>
</div>

第二步 - 添加 CSS:

/* 需要放置覆盖层的容器。根据需要调整宽度 */
.container {
  position: relative;
  width: 50%;
}

/* 使图像可响应 */
.image {
  width: 100%;
  height: auto;
}

/* 叠加效果(全高和全宽)- 位于容器顶部和图像上方 */
.overlay {
  position: absolute;
  bottom: 0;
  left: 0;
  right: 0;
  background-color: #008CBA;
  overflow: hidden;
  width: 100%;
  height: 100%;
  transform: scale(0);
  transition: .3s ease;
}

/* 当您将鼠标悬停在容器上时,叠加文本将“缩放”显示 */
.container:hover .overlay {
  transform: scale(1);
}

/* 覆盖层内的一些文本,垂直和水平方向都居中定位 */
.text {
  color: white;
  font-size: 20px;
  position: absolute;
  top: 50%;
  left: 50%;
  transform: translate(-50%, -50%);
  text-align: center;
}

亲自试一试

相关页面

教程:CSS 图像

教程:如何创建图像叠加悬停效果