如何防止图像超出父元素边框缩放?

3
我有一个不同服务列表,每个服务都有一个图像、标题和描述。我已经设置了当鼠标悬停时,图像缩放到1.3倍大小。缩放功能正常,但它在执行时忽略了父元素的边框。
我已经捕捉了一个GIF来展示它的运行情况。

Example of image scaling outside of parent's borders

这是我正在使用的代码。

.cleaningservices {
    max-width: 250px;
    max-height: 250px;

}

    /* Default state of the image */
    .hover01 img {
      -webkit-transform: scale(1);
      transform: scale(1);
      -webkit-transition: .3s ease-in-out;
      transition: .3s ease-in-out;
    }

    /* Scale the image to 1.3 it's size on mouse hover */
    .hover01:hover img {
      -webkit-transform: scale(1.3);
      transform: scale(1.3);
    }
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap-theme.min.css" rel="stylesheet"/>
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet"/>
<link href="https://cdnjs.cloudflare.com/ajax/libs/bootstrap-select/1.11.2/css/bootstrap-select.min.css" rel="stylesheet"/>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

<div class="row">
  <div class="cleaningservices">
    <div class="col-sm-6 col-md-4">
      <div class="thumbnail hover01">
        <img src="https://bosworthco.com/wp-content/uploads/2016/10/cleaning-268134_960_720.jpg" alt="deep house cleaning">
        <div class="caption">
          <h3>Deep Cleaning</h3>
          <p>Usually, on our first visit we will most likely have to complete a deep cleaning. It is a far more intensive cleaning that our regular cleaning. After first deep cleaning we will continue with one of our regularly scheduled cleanings below.</p>
        </div>
      </div>
    </div>
  </div>
</div>

我需要如何更改代码,使得鼠标悬停时图片在父元素边框内按比例缩放?

2
父元素使用overflow: hidden;属性? - Pepo_rasta
减小缩放因子。 - connexo
给父元素添加 overflow: hidden;。 - Venu Madhav
正如大家所说的,overflow hidden 会帮助你解决问题。 - andy jones
3个回答

3
您可以将图像添加到 div 中,并为该 div 添加 overflow:hidden;

.hover01 img {
    -webkit-transform: scale(1);
    transform: scale(1);
    -webkit-transition: .3s ease-in-out;
    transition: .3s ease-in-out;
    width:100%;
}
.hover01:hover img {
    -webkit-transform: scale(1.3);
    transform: scale(1.3);
}

.img-parent{
    overflow:hidden;
}
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap-theme.min.css" rel="stylesheet"/>
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet"/>
<link href="https://cdnjs.cloudflare.com/ajax/libs/bootstrap-select/1.11.2/css/bootstrap-select.min.css" rel="stylesheet"/>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="row">
   <div class="cleaningservices">
      <div class="col-sm-6 col-md-4">
   <div class="thumbnail hover01">
      <div class="img-parent">
         <img src="http://fiusms.fiu.edu/wp-content/uploads/Residential-Cleaning-Watauga-TX.jpg" alt="deep house cleaning">
             </div>
      <div class="caption">
  <h3>Deep Cleaning</h3>
  <p>Usually, on our first visit we will most likely have to complete a deep cleaning. It is a far more intensive cleaning that our regular cleaning. After first deep cleaning we will continue with one of our regularly scheduled cleanings below.</p>
      </div>
   </div>
       </div>
    </div>
</div>


这可能是最简洁的方法,因为它也避免了在随后的文本中缩放。我会将类称为.noverflow - connexo
谢谢你的回答,确实帮助我达到了我想要的目标。 - raimond

1
在图像容器上设置overflow: hidden;。我在body上添加了背景颜色,以便您看到它的作用。

body {
  background-color: #f0f0f0 !important;
  padding: 50px;
}
.noverflow {
  overflow: hidden;
}
.thumbnail {
  background-color: #fff;
}
.hover01 img {
  -webkit-transform: scale(1);
  transform: scale(1);
  -webkit-transition: .3s ease-in-out;
  transition: .3s ease-in-out;
}
.hover01:hover img {
  -webkit-transform: scale(1.1);
  transform: scale(1.1);
}
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap-theme.min.css" rel="stylesheet" />
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet" />
<link href="https://cdnjs.cloudflare.com/ajax/libs/bootstrap-select/1.11.2/css/bootstrap-select.min.css" rel="stylesheet" />
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="row">
  <div class="cleaningservices">
    <div class="col-sm-6 col-md-4">
      <div class="thumbnail hover01">
        <div class="noverflow"><img src="http://fiusms.fiu.edu/wp-content/uploads/Residential-Cleaning-Watauga-TX.jpg" alt="deep house cleaning"></div>
        <div class="caption">
          <h3>Deep Cleaning</h3>
          <p>Usually, on our first visit we will most likely have to complete a deep cleaning. It is a far more intensive cleaning that our regular cleaning. After first deep cleaning we will continue with one of our regularly scheduled cleanings below.</p>
        </div>
      </div>
    </div>
  </div>
</div>


1

只需使用 overflow: hidden;

.hover01 img {
  -webkit-transform: scale(1);
  transform: scale(1);
  -webkit-transition: .3s ease-in-out;
  transition: .3s ease-in-out;
  overflow: hidden;
}

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接