简体   繁体   English

如果父级没有悬停,Div 不会松开 hover state

[英]Div doesn't loose hover state if parent isn't hovered

If you hover above the second, third or forth item the hidden text shows up on the left side.如果 hover 在第二个、第三个或第四个项目上方,隐藏的文本会显示在左侧。

If you move your cursor to the hidden text, the hidden text will hide again.如果您将 cursor 移动到隐藏文本,隐藏文本将再次隐藏。

But i want that you can hover over the second item, move your cursor to the "hide" and eg click on it.但我希望你可以在第二个项目上使用 hover,将你的 cursor 移动到“隐藏”,然后点击它。 Is this possible in CSS or JS?这在 CSS 或 JS 中是否可行?

The HTML: HTML:

 <div class="container">
    <div class="item">item
       <div class="hide">hide</div>
    </div>
   <div class="item">item <div class="hide">hide</div></div>
   <div class="item">item <div class="hide">hide</div></div>
   <div class="item">item <div class="hide">hide</div></div>
  </div>

The CSS: CSS:

.container {
  display: flex;
  position: relative;
}

.item {
  padding: 1px;
  cursor: pointer;
}

.hide {
    opacity: 0;
    position: absolute;
    left: 0;
}


.item:hover .hide {
  display: block;
  opacity: 1;
}

Here is my codepen这是我的代码笔

I tried to expand the width from the hide element, but that doesn't work我试图从隐藏元素扩展宽度,但这不起作用

.hide {
    width: 100vw;
}

Is there a method to expand the hide elements width or something else, so the "hide" won't go away if the parent element isn't hovered?有没有一种方法可以扩展隐藏元素的宽度或其他东西,所以如果父元素没有悬停,“隐藏”不会 go 消失?

Just tweaked some of the code for you so that the layout worked better.刚刚为您调整了一些代码,以便布局更好地工作。 Main issue looks to be in the last line of CSS I've added where you target to see if the .hide has a hover state triggered.主要问题似乎在 CSS 的最后一行,我添加了您的目标以查看.hide是否触发了hover state。

 .container { display: flex; }.item { padding: 1px; cursor: pointer; float:left; width:100%; }.hide { opacity: 0; position: absolute; left: 0; width:100%; pointer-events:none; }.item:hover >.hide, .item >.hide:hover { display: block; opacity: 1; pointer-events:all; }
 <div class="container"> <div class="item">item <div class="hide">hide1</div> </div> <div class="item">item <div class="hide">hide2</div></div> <div class="item">item <div class="hide">hide3</div></div> <div class="item">item <div class="hide">hide4</div></div> </div>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM