简体   繁体   English

div悬停上可见的按钮

[英]button visible on div hover

I am trying to make the button visible when the user hovers on the div yet not working. 当用户将鼠标悬停在div上却无法正常工作时,我试图使按钮可见。

 #box{ background-color: red; } .btn { position: relative; z-index: 1; display: none; } .foo:hover + .btn{ display: inline-block; } /*ignore from here*/ .foo { position:relative; width:500px; height:500px; } .foo img { width:100%; vertical-align:top; } .foo:after, .foo:before { position:absolute; opacity:0; transition: all 0.3s; -webkit-transition: all 0.3s; } .foo:after { content:'\\A'; width:100%; height:100%; top:0; left:0; background:rgba(0,0,0,0.8); } .foo:before { content: attr(data-content); color:#fff; z-index:1; padding:4px 10px; text-align:center; position: absolute; top: 50%; left: 50%; transform: translate(-50%, -50%); } .foo:hover:after, .foo:hover:before { opacity:1; } /*ignore to here*/ 
 <div id="box" class="foo" data-content="Caption"> <button class="btn">view</button> </div> 

the button display is initially set to none . 该按钮显示最初设置为none and when the users hover on the div it is set to inline-block . 当用户将鼠标悬停在div上时,它将设置为inline-block what can I do to get the button on top and visible when the user hovers on the div 当用户将鼠标悬停在div上时,我该怎么做才能使按钮位于顶部并可见

Just change your + sign to a > sign. 只需将+号更改为>号即可。 The button is inside the div, not on the same level as it. 该按钮位于div内,与其不在同一级别。 + is for items on the same level +表示相同级别的物品

   .foo:hover > .btn{
      display: inline-block;
    }

You're almost there, but to select the button, you need > (child combinator) to select the child of the hovered div . 您快到了,但是要选择按钮,您需要> (子组合器)来选择div的子项。 Use + when selecting subsequent siblings . 选择后续兄弟姐妹时使用+

 /*ignore from here*/ .foo { position: relative; width: 500px; height: 500px; } .foo img { width: 100%; vertical-align: top; } .foo:after, .foo:before { position: absolute; opacity: 0; transition: all 0.3s; -webkit-transition: all 0.3s; } .foo:after { content: '\\A'; width: 100%; height: 100%; top: 0; left: 0; background: rgba(0, 0, 0, 0.8); } .foo:before { content: attr(data-content); color: #fff; z-index: 1; padding: 4px 10px; text-align: center; position: absolute; top: 50%; left: 50%; transform: translate(-50%, -50%); } .foo:hover:after, .foo:hover:before { opacity: 1; } /*ignore to here*/ #box { background-color: red; } .btn { position: relative; z-index: 1; display: none; } .foo:hover>.btn { display: inline-block; } 
 <div id="box" class="foo" data-content="Caption"> <button class="btn">view</button> </div> 

The + combinator selects adjacent siblings. +组合器选择相邻的同级。 This means that the second element directly follows the first, and both share the same parent. 这意味着第二个元素直接跟随第一个元素,并且它们共享相同的父元素。 .btn is'nt sibling for .foo ,but it is his children. .btn不是.foo兄弟,但它是他的孩子。 so remove + selector. 因此删除+选择器。

Change : 变更:

.foo:hover + .btn{

To: 至:

.foo:hover .btn{

 .foo { position:relative; width:500px; height:500px; } .foo img { width:100%; vertical-align:top; } .foo:after, .foo:before { position:absolute; opacity:0; transition: all 0.3s; -webkit-transition: all 0.3s; } .foo:after { content:'\\A'; width:100%; height:100%; top:0; left:0; background:rgba(0,0,0,0.8); } .foo:before { content: attr(data-content); color:#fff; z-index:1; padding:4px 10px; text-align:center; position: absolute; top: 50%; left: 50%; transform: translate(-50%, -50%); } .foo:hover:after, .foo:hover:before { opacity:1; } #box{ background-color: red; } .btn { position: relative; z-index: 1; display: none; } .foo:hover .btn{ display: inline-block; } 
 <div id="box" class="foo" data-content="Caption"> <button class="btn">view</button> </div> 

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

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