放大悬停图像并保持相同的大小

时间:2016-03-03 21:02:10

标签: html css hover image-zoom

我正在尝试为我的网站设置悬停缩放功能,但是,我看到了其他示例,我的代码似乎是相同的,但图像的尺寸并不相同。任何想法可能是什么问题?

.imgBox {
  display: inline-block;
  overflow: hidden !important;
  transition: .3s ease-in-out
}

  .imgBox {
    display: inline-block;
    overflow: hidden !important;
    transition: .3s ease-in-out
  }
  .imgBox:hover {
    opacity: 0.6;
    filter: alpha(opacity=30);
    transform: scale(1.3);
  }
<div class="large-3 medium-6 small-12 columns">
  <h2>Heading 1</h2>
  <img class="imgBox" src="http://techmadeplain.com/img/2014/300x200.png" />
  <p>Paragraph here</p>
</div>

JSFiddle

2 个答案:

答案 0 :(得分:5)

您似乎正在尝试缩放图像,但是要限制实际看到的图像需要在容器中缩放图像

然后,当缩放图像时,容器将隐藏任何通常会溢出的图像&#34;如果你没有隐藏过度的话。

所以,就像这样:

&#13;
&#13;
.imgBox { /* now a container for the image */
    display: inline-block; /* shrink wrap to image */
    overflow: hidden; /* hide the excess */
  }
  .imgBox img {
    display: block; /* no whitespace */
    transition: .3s ease-in-out;
  }
  .imgBox:hover img {
    transform: scale(1.3);
  }
&#13;
<div class="large-3 medium-6 small-12 columns">
  <h2>Heading 1</h2>
  <div class="imgBox">
    <img src="http://lorempixel.com/image_output/food-q-c-300-200-9.jpg" />
  </div>
  <p>Paragraph here</p>
</div>
&#13;
&#13;
&#13;

答案 1 :(得分:1)

保利是对的,但还有一种方法可以使用background-image代替img代码

&#13;
&#13;
  .imageWrapper{
    background-image:url("http://techmadeplain.com/img/2014/300x200.png");
    width:300px;
    height:200px;
    background-size:100%;  
    background-position: center center;
    transition: .3s ease-in-out;
  }

  .imageWrapper:hover{
    background-size:130%;
  }
&#13;
<div class="large-3 medium-6 small-12 columns">
  <h2>Heading 1</h2>
  <div class='imageWrapper'> 
  </div>  
  <p>Paragraph here</p>
</div>
&#13;
&#13;
&#13;