我在同一个位置上有相同大小的div。但是他们没有背景,所以你没有看到元素在不同的容器中。 我现在遇到的问题是,悬停事件仅在最后一个容器中的元素上触发,因为它被分层放在其他容器的顶部。悬停在分层元素上不起作用
#main {
background: yellow;
width: 300px;
height: 300px;
position: absolute;
left: 30px;
top: 30px;
z-index: 1;
}
.out {
width: 300px;
height: 300px;
position: absolute;
left: 0;
top: 0;
background: none;
z-index: 5;
}
.in {
position: absolute;
width: 40px;
height: 40px;
z-index: 10;
opacity: 0.5;
}
.out:nth-of-type(1) .in {
top: 40px;
left: 40px;
background: green;
}
.out:nth-of-type(2) .in {
top: 120px;
left: 120px;
background: red;
}
.out:nth-of-type(3) .in {
top: 200px;
left: 200px;
background: blue;
}
.in:hover {
opacity: 1.0;
}
<div id="main">
<div class="out">
<div class="in">DIV 1</div>
</div>
<div class="out">
<div class="in">DIV 2</div>
</div>
<div class="out">
<div class="in">DIV 3</div>
</div>
</div>
是否有可能迫使悬停事件还是我必须把所有的元素融入同一容器(这将是可能的,但不是说好原来的网站上) ?
我知道解释不是最好的,但我认为你应该明白这个代码。 JSFiddle