简体   繁体   English

将鼠标悬停在元素上并同时显示另一个元素

[英]Hovering on element and making another element appear at the same time

I've been trying to figure this out for a long time and I can't seem to get it. 我已经尝试解决了很长时间,但似乎无法理解。 I have the following HTML: 我有以下HTML:

<div class="b">
    <button>Show when I hover</button>
</div>
<div class="A">When I hover over this the background should change</div>

with the corresponding CSS: 与相应的CSS:

.b {
    float: right;
    display: none;
}
.A {
    float: left;
    display: inline;
    width: 1000px;
}

.A:hover {
    background: gray;
}

.A:hover + .b {
    display: block;
}

What I'm trying to do is whenever I hover over A the b div and corresponding button should show. 我想做的是每当我将鼠标悬停在Ab div和相应的按钮应该显示。 In addition, I want it such that when my mouse is on the button, the background of A is still gray as if I was hovering over it. 另外,我希望它使鼠标在按钮上时, A的背景仍然是灰色的,就像我将鼠标悬停在它上面一样。 I can't seem to figure this out. 我似乎无法弄清楚。 Any ideas? 有任何想法吗?

Relevant JSFiddle: http://jsfiddle.net/sn19k1wz/3/ 相关的JSFiddle: http : //jsfiddle.net/sn19k1wz/3/

You can do this by changing position of A and B 您可以通过更改AB位置来做到这一点

<div class="A">When I hover over this the background should change</div>
<div class="b">
    <button>Show when I hover</button>
</div>

Change the div positions, hovering div tag should be the first one 更改div的位置,将div标记悬停在第一个

Like this : 像这样 :

<div class="A">When I hover over this the background should change</div>
<div class="b">
    <button>Show when I hover</button>
</div>

Demo URL 演示网址

Try like this: Demo 尝试这样: 演示

 .A {

    display: inline-block;
    width: 1000px;
      position: relative;
}
.b {   
    display: none;
    position: absolute;
    z-index: 999;
    right: 0;
    top:6px;
}
.A:hover {
    background: gray;
}
.A:hover + .b {
    display: block;
    background: red;
    cursor:pointer;
}

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

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