图像悬停效果扩展到容器外

时间:2016-07-20 16:14:27

标签: html css hover css-transitions

我目前有一个图像网格,其中应用了“灰度到颜色”和“缩放”效果。缩放效果正在容器外扩展。我已应用overflow: hidden(几次)并设置max-width。但是,这些尝试都没有成功。这是我的JSFiddle

HTML

<div class="content">
  <ul class="rows">
    <li>
      <a href="#"><img src="http://lorempixel.com/400/200/" alt="Case Study" width="50%" height="auto" style="float:left"></a>
    </li>
    <li>
      <a href="#"><img src="http://lorempixel.com/400/200/" alt="Case Study" width="50%" height="auto" style="float:left"></a>
    </li>
    <li>
      <a href="#"><img src="http://lorempixel.com/400/200/" alt="Case Study" width="50%" height="auto" style="float:left"></a>
    </li>
    <li>
      <a href="#"><img src="http://lorempixel.com/400/200/" alt="Case Study" width="50%" height="auto" style=" float:left"></a>
    </li>
    <li>
      <a href="#"><img src="http://lorempixel.com/400/200/" alt="Case Study" width="50%" height="auto" style="float:left"></a>
    </li>
    <li>
      <a href="#"><img src="http://lorempixel.com/400/200/" alt="Case Study" width="50%" height="auto" style="float:left"></a>
    </li>
  </ul>
</div>

CSS

.content {
  width: 100%;
  height: auto;
  min-height: 100%;
}

.rows {
  list-style: none;
  overflow: hidden;
  width: 100%;
}

.rows li a img {
  max-width: 100%;
  overflow: hidden;
}

.rows a img {
  -webkit-transform: scale(1);
  transform: scale(1);
  -webkit-transition: .3s ease-in-out;
  transition: .3s ease-in-out;
  -webkit-filter: grayscale(100%) opacity(0.7);
  filter: grayscale(100%) opacity(0.7);
}

.rows a img:hover {
  -webkit-transform: scale(1.2);
  transform: scale(1.2);
  -webkit-filter: grayscale(0) opacity(1);
  filter: grayscale(0) opacity(1);
  -webkit-transition: .3s ease-in-out;
  transition: .3s ease-in-out;
}

1 个答案:

答案 0 :(得分:1)

图片容器为a标记,因此您应在此元素上设置overflow: hidden

.rows li a {
  float: left;
  width: 50%;
  overflow: hidden;
}

将以上代码添加到您的CSS并将所有图片宽度设置为html 100%

<a href="#"><img src="http://lorempixel.com/400/200/" alt="Case Study" width="100%" height="auto" style="float:left"></a>