CSS 变换比例破坏图像叠加
CSS transform scale breaks image overlay
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来结合这两个效果?
您可以尝试将 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>
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来结合这两个效果?
您可以尝试将 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>