图像被灰色框(带有数字)隐藏,其大小与图像相同。当悬停在上方时,灰色会淡化并显示图像,过了一会儿,文本会在图像顶部淡入。
我开始倒退,在我写出"框的淡出之前写下了文字的淡入。"但是,框(图中)的内容放在figcaption标签内,并按其规则设置。为什么会发生这种情况,是否有解决问题的方法?
以下是代码的相关部分。
section figure {
counter-increment: numImg;
display: flex;
position: relative;
}
section figure::before {
background: rgba(0, 0, 0, 0.5);
content: counter(numImg);
position: absolute;
top: 0;
left: 0;
font-size: 2rem;
color: #0e533e;
width: 200px;
height: 200px;
z-index: 3;
line-height: 200px;
text-align: center;
}
section figure:hover figcaption {
transition: opacity .7s ease-in-out;
opacity: 1;
}
section figure figcaption {
text-shadow: 0px 0px 2px white;
font-size: 2em;
text-align: center;
align-content: center;
width: 200px;
z-index: 1;
position: absolute;
top: -0px;
opacity: 0;
}

<section>
<figure>
<img src="http://cheb-room.ru/uploads/cheb/2016/11/w9RC4W-QqXw-200x200.jpg" alt="">
<figcaption>Text that should fade in</figcaption>
</figure>
</section>
&#13;
我到目前为止唯一发现的是,不透明度会影响容器中的所有内容(也就是说,在figcaption中),但不能避免我的计数器在中显示 figcaption。
答案 0 :(得分:1)
据我所知,伪前对象没有出现在figcaption元素内部,而是出现在figure元素内部。我为before元素添加了一个悬停状态,以便在文本的同时将其淡出,因此它看起来像你想要的那样。
section figure {
counter-increment: numImg;
display: flex;
position: relative;
}
section figure:before {
background: rgba(0, 0, 0, 0.5);
content: counter(numImg);
position: absolute;
top: 0;
left: 0;
font-size: 2rem;
color: #0e533e;
width: 200px;
height: 200px;
z-index: 3;
line-height: 200px;
text-align: center;
transition: opacity .7s ease-in-out;
opacity: 1;
}
section figure figcaption {
text-shadow: 0px 0px 2px white;
font-size: 2em;
text-align: center;
align-content: center;
width: 200px;
z-index: 1;
position: absolute;
top: -0px;
opacity: 0;
transition: opacity .7s ease-in-out;
}
section figure:hover:before {
opacity: 0;
}
section figure:hover figcaption {
opacity: 1;
}
&#13;
<section>
<figure>
<img src="https://placehold.it/200/1E5799/ffffff" alt="FPO">
<figcaption>Text that should fade in</figcaption>
</figure>
</section>
&#13;