英文:
Image zoom on hover not working with :after
问题
我只想为我的图像添加一个缩放效果和透明覆盖层,我尝试了这种方式,但只有覆盖层有效。图像缩放效果不起作用。我该如何修复这个问题?
.image-box img {
  position: relative;
  transition: all 500ms ease;
}
.image-box {
  overflow: hidden;
}
.image-box img:hover {
  transform:scale(1.1); 
}
.image-box:after {
  content: '\A';
  position: absolute;
  width: 100%;
  height: 100%;
  top: 0;
  left: 0;
  z-index: 2;
  background: rgba(0, 0, 0, 0.4);
  opacity: 0;
  transition: all 500ms ease;
}
.image-box:hover:after {
  opacity: 1;
}
<div class="inner-box">
  <figure class="image-box">
    <img width="480" height="480" src="https://4.img-dpreview.com/files/p/E~TS590x0~articles/3925134721/0266554465.jpeg">
  </figure>
</div>
英文:
I just want to add a zoom effect and transparent overlay into my image, I tried this way but working only the overlay. Image zoom effect isn't working. How can I fix this?
<!-- begin snippet: js hide: false console: true babel: false -->
<!-- language: lang-css -->
.image-box img {
position: relative;
transition: all 500ms ease;
}
.image-box {
overflow: hidden;
}
.image-box img:hover {
transform:scale(1.1);
}
.image-box:after {
content: '\A';
position: absolute;
width: 100%;
height: 100%;
top: 0;
left: 0;
z-index: 2;
background: rgba(0, 0, 0, 0.4);
opacity: 0;
transition: all 500ms ease;
}
.image-box:hover:after {
opacity: 1;
}
<!-- language: lang-html -->
<div class="inner-box">
  <figure class="image-box">
    <img width="480" height="480" src="https://4.img-dpreview.com/files/p/E~TS590x0~articles/3925134721/0266554465.jpeg">
  </figure>
</div>
<!-- end snippet -->
答案1
得分: 3
The overlay itself prevents the hover event from propagating to the img.
一个解决方法是不在img上使用:hover,而是在image-box上悬停时放大img。
.image-box img{
    position: relative;
    transition: all 500ms ease;
}
.image-box{
    overflow: hidden;
}
.image-box:hover img{       /* changed */
    transform:scale(1.1); 
}
 .image-box:after{
    content:'\A';
    position:absolute;
    width:100%;
    height:100%;
    top:0;
    left:0;
     z-index: 2;
    background: rgba(0, 0, 0, 0.4);
    opacity:0;
    transition: all 500ms ease;
}
 .image-box:hover:after{
    opacity:1;
}
<div class="inner-box">
  <figure class="image-box">
    <img width="480" height="480" src="https://4.img-dpreview.com/files/p/E~TS590x0~articles/3925134721/0266554465.jpeg">
  </figure>
</div>
英文:
The overlay itself prevents the hover event from propagating to the img.
One solution is to not use :hover on the img, but to grow the img on hovering the image-box.
<!-- begin snippet: js hide: false console: true babel: null -->
<!-- language: lang-css -->
.image-box img{
	position: relative;
	transition: all 500ms ease;
}
.image-box{
	overflow: hidden;
}
.image-box:hover img{       /* changed */
	transform:scale(1.1); 
}
 .image-box:after{
    content:'\A';
    position:absolute;
    width:100%;
	height:100%;
    top:0;
	left:0;
	 z-index: 2;
    background: rgba(0, 0, 0, 0.4);
    opacity:0;
    transition: all 500ms ease;
}
 .image-box:hover:after{
    opacity:1;
}
<!-- language: lang-html -->
<div class="inner-box">
  <figure class="image-box">
    <img width="480" height="480" src="https://4.img-dpreview.com/files/p/E~TS590x0~articles/3925134721/0266554465.jpeg">
  </figure>
</div>
<!-- end snippet -->
答案2
得分: 2
只需添加以下类:
.image-box:hover img{
    transform:scale(1.1);
    transition: all 500ms ease;
}
.image-box img{
    position: relative;
    transition: all 500ms ease;
}
.image-box{
    overflow: hidden;
}
.image-box img:hover{
    transform:scale(1.1);
}
.image-box:after{
    content: '\A';
    position:absolute;
    width:100%;
    height:100%;
    top:0;
    left:0;
    z-index: 2;
    background: rgba(0, 0, 0, 0.4);
    opacity:0;
    transition: all 500ms ease;
}
.image-box:hover:after{
    opacity:1;
}
.image-box:hover img{
    transform:scale(1.1);
    transition: all 500ms ease;
}
<div class="inner-box">
    <figure class="image-box">
        <img width="480" height="480" src="https://4.img-dpreview.com/files/p/E~TS590x0~articles/3925134721/0266554465.jpeg">
    </figure>
</div>
英文:
Just add the following class:
.image-box:hover img{
    transform:scale(1.1);
    transition: all 500ms ease;
}
<!-- begin snippet: js hide: false console: true babel: false -->
<!-- language: lang-css -->
.image-box img{
    position: relative;
    transition: all 500ms ease;
}
.image-box{
    overflow: hidden;
}
.image-box img:hover{
    transform:scale(1.1);
}
.image-box:after{
    content:'\A';
    position:absolute;
    width:100%;
    height:100%;
    top:0;
    left:0;
    z-index: 2;
    background: rgba(0, 0, 0, 0.4);
    opacity:0;
    transition: all 500ms ease;
}
.image-box:hover:after{
    opacity:1;
}
.image-box:hover img{
    transform:scale(1.1);
    transition: all 500ms ease;
}
<!-- language: lang-html -->
<div class="inner-box">
  <figure class="image-box">
    <img width="480" height="480" src="https://4.img-dpreview.com/files/p/E~TS590x0~articles/3925134721/0266554465.jpeg">
  </figure>
</div>
<!-- end snippet -->
答案3
得分: 0
我认为你的演示效果很完美,你只需要在.image-box:after中做一个小改动。我已经将z-index更新为-1。
.image-box img {
  position: relative;
  transition: all 500ms ease;
}
.image-box {
  overflow: hidden;
}
.image-box:hover img {
  transform: scale(1.1);
}
.image-box:after {
  content: "";
  position: absolute;
  width: 100%;
  height: 100%;
  top: 0;
  left: 0;
  z-index: -1;
  background: rgba(0, 0, 0, 0.4);
  opacity: 0;
  transition: all 500ms ease;
}
.image-box:hover:after {
  opacity: 1;
}
<div class="inner-box">
  <figure class="image-box">
    <img width="480" height="480" src="https://4.img-dpreview.com/files/p/E~TS590x0~articles/3925134721/0266554465.jpeg">
  </figure>
</div>
希望这有所帮助。
英文:
I think your demo works perfectly you just need a minor change in .image-box:after. I have updated z-index to -1.
<!-- begin snippet: js hide: false console: true babel: false -->
<!-- language: lang-css -->
.image-box img {
  position: relative;
  transition: all 500ms ease;
}
.image-box {
  overflow: hidden;
}
.image-box:hover img {
  transform: scale(1.1);
}
.image-box:after {
  content: "";
  position: absolute;
  width: 100%;
  height: 100%;
  top: 0;
  left: 0;
  z-index: -1;
  background: rgba(0, 0, 0, 0.4);
  opacity: 0;
  transition: all 500ms ease;
}
.image-box:hover:after {
  opacity: 1;
}
<!-- language: lang-html -->
<div class="inner-box">
  <figure class="image-box">
    <img width="480" height="480" src="https://4.img-dpreview.com/files/p/E~TS590x0~articles/3925134721/0266554465.jpeg">
  </figure>
</div>
<!-- end snippet -->
I hope this helps.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。


评论