[英]Hover in CSS opacity sibling child element
I'm trying to make images semi-transparent on hover, and display a text on them. 我试图将图像悬停在半透明状态,并在其上显示文本。
I've implemented a pure CSS solution, but there is a problem with it : when I hover the image, the text (a link) shows, but if I get my mouse over this link, it looses the hover on the image, and starts blinking because it is caught on a loop of hover, "unhover". 我已经实现了一个纯CSS解决方案,但是有一个问题:当我将鼠标悬停在图像上时,会显示文本(链接),但是如果我将鼠标悬停在此链接上,它将使图像上的鼠标悬停,开始闪烁,因为它陷入了悬停循环“ unhover”。
An example will be much more explicit so here is my code : http://jsfiddle.net/zm2Jt/3/ 一个示例将更加明确,因此这是我的代码: http : //jsfiddle.net/zm2Jt/3/
CSS part : CSS部分:
.fadeImg {
opacity: 1;
}
.fadeImg~.hoverDisplay {
display: none;
}
.fadeImg:hover {
opacity: 0.5;
}
.fadeImg:hover~.hoverDisplay {
display: block;
}
The text is placed over the image using position absolute. 文本使用绝对位置放置在图像上。 As the link is child of .hoverDisplay, the span that is absolutely positionned, I think I can't do much with CSS to prevent the hovering on the link.
由于链接是.hoverDisplay的子元素,因此绝对确定了范围,我想我不能对CSS进行太多操作以防止在链接上进行悬停。
I know I could add JS for a workaround (adding a class when I enter the image, removing when I leave it), but I would rather go with a CSS although I suspect it's not possible. 我知道我可以为解决方法添加JS(在输入图像时添加类,在离开图像时删除类),但是我宁愿使用CSS,尽管我怀疑这是不可能的。
Thanks for your answers :-) 感谢您的回答:-)
How about chopping down your CSS to a great extent? 如何在很大程度上削减CSS?
Demo 2 ( a
tag inside the box, you can tweak around) 演示2 (
a
盒子里面的标签,你可以调整左右)
.formContainer .teamImg {
float: left;
margin-left: 20px;
position: relative;
width: 300px;
}
.teamImg span {
opacity: 0;
height: 100%;
width: 100%;
position: absolute;
background: rgba(255,255,255,.5);
top: 0;
left: 0;
-webkit-transition: all .5s;
-moz-transition: all .5s;
transition: all .5s;
}
.teamImg:hover span {
opacity: 1;
}
Am just positioning span
tag absolute
to the container element, also am transitioning the opacity here but am also using rgba()
to make the element background slightly opaque. 既可以将
span
标签absolute
地span
在容器元素上,也可以在此处转换不透明度,还可以使用rgba()
来使元素背景稍微不透明。
Made from scratch, simplifying the idea 从头开始,简化了想法
<div>
<img src="URL_HERE" />
<span>Write anything here</span>
</div>
div {
float: left;
border: 1px solid #aaa;
position: relative;
}
div span {
position: absolute;
height: 100%;
width: 100%;
background: rgba(255,255,255,.5);
top: 0;
left: 0;
opacity: 0;
-webkit-transition: all .5s;
-moz-transition: all .5s;
transition: all .5s;
}
div:hover span {
opacity: 1;
}
Add pointer-events: none;
添加
pointer-events: none;
on your text. 在您的文字上。
This way, it shouldn't affect your hover. 这样,它就不会影响您的悬停。
For more informations on pointer-events: 有关指针事件的更多信息:
https://developer.mozilla.org/en-US/docs/Web/CSS/pointer-events?redirectlocale=en-US&redirectslug=CSS%2Fpointer-events https://developer.mozilla.org/en-US/docs/Web/CSS/pointer-events?redirectlocale=en-US&redirectslug=CSS%2Fpointer-events
A little adjustment on the hover states: http://jsfiddle.net/zm2Jt/8/ 对悬停状态进行一些调整: http : //jsfiddle.net/zm2Jt/8/
.fadeImg:hover, .teamImg:hover .fadeImg {
opacity: 0.5;
}
.fadeImg:hover~.hoverDisplay, .teamImg:hover .hoverDisplay {
display: block;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.