我有以下设置:
.container .option {
position: absolute;
display: none;
background-color: gray;
padding: 5px;
}
.container:hover .option {
display: block;
}
<div class="container">
<div class="card">
<p>Hover here</p>
</div>
<div class="option">
<input type="checkbox" />
<label>Option 1</label>
</div>
<div class="option">
<input type="checkbox" />
<label>Option 2</label>
</div>
</div>
<div>
Other content
</div>
https://plnkr.co/edit/AbLUqAkMT9DyJqr0D1oc?p=preview
如果position:absolute
选择器上有.container .option
,则悬停的内容会正确地浮动在“其他内容”div上。但是,我无法访问悬停div中的复选框。如果我删除position:absolute
我可以访问复选框,但内容无法正常浮动。
答案 0 :(得分:1)
更改一点标记和样式,如下所示:
<div class="container">
<div class="card">
<p>Hover here</p>
</div>
<div class="popup">
<div class="option">
<input type="checkbox"/>
<label>Option 1</label>
</div>
<div class="option">
<input type="checkbox"/>
<label>Option 2</label>
</div>
</div>
</div>
<div>
Other content
</div>
.container {
position:relative;
}
.container .popup {
position: absolute;
display: none;
background-color: gray;
padding: 5px;
}
.container:hover .popup {
display: block;
left:0px;
top:100%;
}
答案 1 :(得分:0)
我认为这就是你想要实现的目标
<div class="container">
<div class="card">
<p>Hover here</p>
</div>
<div class="option">
<input type="checkbox"/>
<label>Option 1</label>
</div>
<div class="option">
<input type="checkbox"/>
<label>Option 2</label>
</div>
</div>
<div class="other">
Other content
</div>
.container .option {
display: none;
padding: 5px;
}
.container{
position: absolute;
background-color: gray;
top: 0;
}
.container:hover .option {
display: block;
}
.other{
margin-top: 50px;
}
答案 2 :(得分:0)
如果你问我这个解决方案更清洁,因为
.container>.card .hoverme
{
display:none;
position:absolute;
background:#ccc;
}
.container>.card:hover .hoverme
{
display:inline;
}
&#13;
<div class="container">
<div class="card">
<div class="hoverme">
<div class="option">
<label><input type="checkbox"/>
Option 1</label>
</div>
<div class="option">
<label><input type="checkbox"/>
Option 2</label>
</div>
</div>
<p>Hover here</p>
</div>
</div>
<div>
Other content
</div>
&#13;