图像在悬停时不会缩放,使用:after不起作用。

huangapple go评论83阅读模式
英文:

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

&lt;div class=&quot;inner-box&quot;&gt;
  &lt;figure class=&quot;image-box&quot;&gt;
    &lt;img width=&quot;480&quot; height=&quot;480&quot; src=&quot;https://4.img-dpreview.com/files/p/E~TS590x0~articles/3925134721/0266554465.jpeg&quot;&gt;
  &lt;/figure&gt;
&lt;/div&gt;

<!-- 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:&#39;\A&#39;;
    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 -->

&lt;div class=&quot;inner-box&quot;&gt;
  &lt;figure class=&quot;image-box&quot;&gt;
    &lt;img width=&quot;480&quot; height=&quot;480&quot; src=&quot;https://4.img-dpreview.com/files/p/E~TS590x0~articles/3925134721/0266554465.jpeg&quot;&gt;
  &lt;/figure&gt;
&lt;/div&gt;

<!-- 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:&#39;\A&#39;;
    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 -->

&lt;div class=&quot;inner-box&quot;&gt;
  &lt;figure class=&quot;image-box&quot;&gt;
    &lt;img width=&quot;480&quot; height=&quot;480&quot; src=&quot;https://4.img-dpreview.com/files/p/E~TS590x0~articles/3925134721/0266554465.jpeg&quot;&gt;
  &lt;/figure&gt;
&lt;/div&gt;

<!-- 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: &quot;&quot;;
  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 -->

&lt;div class=&quot;inner-box&quot;&gt;
  &lt;figure class=&quot;image-box&quot;&gt;
    &lt;img width=&quot;480&quot; height=&quot;480&quot; src=&quot;https://4.img-dpreview.com/files/p/E~TS590x0~articles/3925134721/0266554465.jpeg&quot;&gt;
  &lt;/figure&gt;
&lt;/div&gt;

<!-- end snippet -->

I hope this helps.

huangapple
  • 本文由 发表于 2020年1月6日 14:55:47
  • 转载请务必保留本文链接:https://go.coder-hub.com/59607887.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定