簡體   English   中英

CSS 放大圖像懸停

[英]CSS zoom in on image hover

使用以下代碼,我可以在懸停時放大圖像:

 .image { width: 250px; height: 250px; background-image: url(http://duncanlock.net/images/posts/better-figures-images-plugin-for-pelican/dummy-200x200.png); background-repeat: no-repeat; background-position: center; background-size: cover; border: 1px solid red; cursor: pointer; } .image:hover { background-size: 120% 120%; }
 <div class="image"></div>

如何使用<img>標簽而不是帶有背景圖像的<div>來做到這一點?

您可以將圖像包裝在 div 中:

<div class="item">
  <img src="http://via.placeholder.com/200x200">
</div>

並應用此 CSS:

.item { //This is the container div
  border: 1px solid red;
  overflow: hidden;
  width: 200px;
  height: 200px;
}
.item img { //This is the img
  transition: all 0.3s;
}
.item:hover img { //This is the img during hover
  transform: scale(1.5); //Increase or decrease this number to change the zoom
}

 .item { border: 1px solid red; overflow: hidden; width: 200px; height: 200px; } .item img { transition: all 0.3s; } .item:hover img { transform: scale(1.5); }
 <div class="item"> <img src="http://via.placeholder.com/200x200"> </div>

為了能夠在插入帶有<img>的圖像的情況下放大懸停,您需要使用變換和過渡來平滑縮放。

 #imageZoom{ overflow:hidden; transition: all 1s; width:200px; height:200px; } #imageZoom img:hover{ transform: scale(1.2); }
 <div id="imageZoom"> <img src="http://duncanlock.net/images/posts/better-figures-images-plugin-for-pelican/dummy-200x200.png"> </div>

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM