2016-11-29 89 views
2

我提供了不同服务的列表,每个服务都有一个图像,标题和说明。我已经设置了它,以便在鼠标悬停时将图像缩放为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;'on parent? –

+0

减小比例因子。 – connexo

+0

给溢出:隐藏;给父母。 –

回答

1

您可以将图像添加到一个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"> 
 
\t <div class="thumbnail hover01"> 
 
\t  <div class="img-parent"> 
 
\t   <img src="http://fiusms.fiu.edu/wp-content/uploads/Residential-Cleaning-Watauga-TX.jpg" alt="deep house cleaning"> 
 
      </div> 
 
\t  <div class="caption"> 
 
\t \t <h3>Deep Cleaning</h3> 
 
\t \t <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> 
 
\t  </div> 
 
\t </div> 
 
     </div> 
 
    </div> 
 
</div>

+0

这可能是最简单的方法,因为它也避免了对后续文本的缩放。我会叫'.noverflow'类。 – connexo

+0

是的..你需要另一个div来包含图像 – Chiller

+0

谢谢你的回答,它确实帮助我实现了我想要的。 – raimond

1

创建一个img容器,您在其中设置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>

+0

确实有用,谢谢你的回答。 – raimond

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; 
}