[英]How do I keep a CSS hover rule applied when the mouse moves to the child element?
I have a table that when you hover each row a delete button slides out from under the last cell in the row. 我有一张桌子,当您将鼠标悬停在每行上时,删除按钮将从该行的最后一个单元格滑出。 The problem is when I move off of the row to click on the delete button the delete button slides back under the cell because the :hover rule is no longer being applied. 问题是,当我离开该行单击删除按钮时,该删除按钮会向后滑动到单元格下方,因为:hover规则不再适用。
Here is my code: 这是我的代码:
table { margin: 10px auto; background-color: white; border-collapse: collapse; } body { font-family: helvetica; } p { margin: 10px auto; width: 200px; } td { padding: 10px; border: 1px solid #333; position: relative; } .delete { position: absolute; width: 20px; height: 20px; top: 50%; left: 100%; margin-top: -10px; margin-left: -20px; background-color: hsl(0,70%,50%); border-radius: 20px; z-index: -1; transition: 0.3s; } tr:hover .delete { margin-left: 2px; }
<p>hover the rows</p> <table> <tr> <td>Test Data</td> <td>More Data<div class="delete"></div></td> </tr> <tr> <td>More Data</td> <td>Test Data<div class="delete"></div></td> </tr> </table>
Use a dummy-cell for the delete-button and hide with left: 0%
, margin-left:-20px;
对删除按钮使用一个虚拟单元格,并left: 0%
隐藏left: 0%
, margin-left:-20px;
and overflow: hidden
on the dummy-cell: 和overflow: hidden
在虚拟单元格上:
<tr>
<td>Test Data</td>
<td>More Data</td>
<td class="dummy"><div class="delete"></div></td>
</tr>
.dummy {
width: 20px;
overflow:hidden;
border: none;
}
.delete {
position: absolute;
width: 20px;
height: 20px;
top: 50%;
left: 0%;
margin-top: -10px;
margin-left: -20px;
background-color: hsl(0,70%,50%);
border-radius: 20px;
transition: 0.3s;
}
The reason why you are losing your hover on the div
is because of the negative z-index
. 之所以要在div
上失去悬停,是因为z-index
为负。
All you have to do, is to also change the z-index
along with the margin
on :hover
. 您要做的就是更改z-index
以及:hover
上的margin
。 That's it. 而已。 no other changes required. 无需其他更改。 Although, you might want to increase the size of the .delete
a little to be able to capture it within the height of the tr
. 虽然,您可能希望稍微增加.delete
的大小,以便能够在tr
的高度内捕获它。
Snippet: 片段:
table { margin: 10px auto; background-color: white; border-collapse: collapse; } body { font-family: helvetica; } p { margin: 10px auto; width: 200px; } td { padding: 10px; border: 1px solid #333; position: relative; } .delete { position: absolute; top: 50%; left: 100%; width: 20px; height: 20px; margin-top: -10px; margin-left: -20px; background-color: hsl(0,70%,50%); border-radius: 20px; z-index: -1; transition: 0.3s; } tr:hover .delete { margin-left: 2px; z-index: auto; }
<p>hover the rows</p> <table> <tr><td>Test Data</td><td>More Data<div class="delete"></div></td></tr> <tr><td>More Data</td><td>Test Data<div class="delete"></div></td></tr> </table>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.