CSS变换缩放破坏图像叠加

4

HTML标记:

<div class="item">
    <a href="url">
        <img src="photo.jpg" />
    </a>
</div>

这个CSS在鼠标悬停时放大图片:

.item a img {
   transition: all 0.2s linear;
}

.item a:hover img {
   transform: scale(1.05);
}

这段 CSS 代码在鼠标悬停时添加了一个覆盖层:

.item a {
    position: relative;
}

.item a:before {
    content: "";
    display: block;
    position: absolute;
    top: 0;
    bottom: 0;
    left: 0;
    right: 0;
    background: none no-repeat center center;
    transition: all .3s linear;
}
.item a:hover:before {
    background:rgba(0,0,0, 0.5) url('img/zoom.png') no-repeat center center;
}

每个效果单独使用都没有问题。但是,当我尝试同时使用这两个CSS时,只有缩放效果有效,叠加效果无法正常工作。

简单来说,我该如何编写CSS以结合这两个效果?

1个回答

4
您可以尝试在绝对定位的元素上添加z-index:1

.item a img {
  transition: all 0.2s linear;
}
.item a:hover img {
  transform: scale(1.05);
  vertical-align: top;
}
.item a {
  position: relative;
  display: inline-block;
}
.item a:before {
  content: "";
  display: block;
  position: absolute;
  top: 0;
  bottom: 0;
  left: 0;
  right: 0;
  background: none no-repeat center center;
  transition: all .3s linear;
  z-index: 1;
}
.item a:hover:before {
  background: rgba(0, 0, 0, 0.5) url('//dummyimage.com/50') no-repeat center center;
}
<div class="item">
  <a href="url">
    <img src="//dummyimage.com/200" />
  </a>
</div>


1
简单的修复,虽然我不需要 vertical-align:topdisplay:inline-block... 我只是添加了 z-index:1。谢谢! - Sparky

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