当我需要将div设置为60%不透明度时,问题开始了,当悬停时它的不透明度为90%。
唯一的问题是我需要一个全白色(不透明)的PNG图像在这个框的顶部。
所以我尝试了一个技巧,覆盖一个单独的div包含图像,并使用空白来把它带到位置;但是背景div动画悬停不工作,当你的鼠标在图像的顶部。
HTML<div style="position:relative;top:-1px;left:0">
<div class="ontop"><img src="http://www.designdownloader.com/item/pngs/button01_google/button01_google-20110813210436-00005.png" alt="OneSpring - Play Video" /></div>
<div id="box-video">
</div>
</div>
</div>
CSS #box-video {
position: absolute;
background-color:rgba(0,57,129,1);
top: 0;
left: 0;
padding: 15px;
font-family: 'Helvetica Neue Light', Arial, Helvetica, sans-serif;
width: 210px;
height: 130px;
opacity: 0.6;
filter: alpha(opacity=60);
-webkit-transition: opacity .25s ease-in-out;
-moz-transition: opacity .25s ease-in-out;
-ms-transition: opacity .25s ease-in-out;
-o-transition: opacity .25s ease-in-out;
transition: opacity .25s ease-in-out;
}
#box-video:hover, .ontop:hover {
cursor: pointer;
/*color: #ffffff;*/
text-decoration: none;
opacity: 0.9;
filter: alpha(opacity=90);
zoom: 1;
-webkit-transition: opacity .25s ease-in-out;
-moz-transition: opacity .25s ease-in-out;
-ms-transition: opacity .25s ease-in-out;
-o-transition: opacity .25s ease-in-out;
transition: opacity .25s ease-in-out;
}
div.ontop {
position: relative;
top: 4.7em;
left:30px;
z-index:1002;
}
下面是显示问题的JSFiddle:
http://jsfiddle.net/xpancom/fZrWA/如何使背景悬停工作,即使你是在图像的顶部?
您可以选择使用:before或:after伪类。
它将大大清理你的代码。
你的HTML应该是这样的:
<div style="position:relative;top:-1px;left:0">
<div id="box-video"></div>
</div>
这里有更多关于他们的信息,这里是小提琴:http://jsfiddle.net/jyHMf/
我想你说的是CSS pointer-events
属性。pointer-events | MDN
div.ontop {
position: relative;
top: 4.7em;
left:30px;
z-index:1002;
pointer-events: none;
}
http://jsfiddle.net/8CZEY/为容器添加id:
<div id="box" style="position:relative;top:-1px;left:0">
现在把hover
事件放在容器上,让它影响子元素:
#box:hover {
cursor: pointer;
}
#box:hover #box-video {
opacity: 0.9;
filter: alpha(opacity=90);
zoom: 1;
-webkit-transition: opacity .25s ease-in-out;
-moz-transition: opacity .25s ease-in-out;
-ms-transition: opacity .25s ease-in-out;
-o-transition: opacity .25s ease-in-out;
transition: opacity .25s ease-in-out;
}
演示:http://jsfiddle.net/fZrWA/2/