简体   繁体   English

当鼠标移动到子元素时,如何保持CSS悬停规则适用?

[英]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在虚拟单元格上:

JSFiddle JSFiddle

<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.

相关问题 css悬停元素来显示孩子,当我将鼠标悬停在孩子身上时,如何将悬停css保留在父母身上? - css hover element to show child, how can I keep the hover css on the parent when I mouseover the child? 将样式应用于元素上的:hover(最好在Firebug中)时,如何调试CSS? - How do I debug CSS, when a style is applied to element on :hover over element's parent (preferably in Firebug)? 将鼠标悬停在子元素CSS上时,保持父对象处于活动状态 - Keep parent active when hover on the child element CSS 将鼠标悬停在父元素上时,如何更改子元素? - How can I make the child element change when hover is applied on the parent? 我怎么知道什么时候应该将CSS动画应用于包含元素而不是直接应用于元素? - How do I know when a CSS animation should be applied to the containing element vs applied directly to the element? 当将鼠标悬停在其子元素上时,如何保持父元素的背景颜色不变? - how can I keep parent element's background-color unchanged when hover on its child element? 当鼠标移到超链接时,如何更改其可见性? - How do I change the visibility of a hyperlink when the mouse moves over it? 如何防止子元素继承主容器的悬停元素 - How do I keep the child elements from inheriting the main container's hover element 元素移动时如何删除悬停状态 - How to remove hover state when the element moves 如何在元素上添加类并保留在那里,直到鼠标离开animate.css为止? - How do I add a class to an element and keep it there until the mouse leaves with animate.css?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM