如何創建:圖像疊加縮放
學習如何在懸停時創建圖像疊加縮放效果。
圖像疊加全屏縮放
請將鼠標懸停在圖像上,即可查看縮放效果。

如何創建疊加縮放效果
第一步 - 添加 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 圖像
教程:如何創建圖像疊加懸停效果