简体   繁体   English

将鼠标悬停在/点击以影响另一个div?

[英]Hover over/click on div to affect another div?

I'm trying to affect 3 other div's when I hover over/click on one but it doesn't want to work. 当我将鼠标悬停在其上并点击其中时,我正试图影响其他3个div,但它不想工作。

#header2:hover {
background-color: rgba(10,50,10,0.9);
}
#header2:active {
    background-color: rgba(0,0,0,0);
}
#header2:active ~ #header1{
    background-color: rgba(0,0,0,0.8);
}
#header2:active ~ #header3{
    background-color: rgba(50,10,10,0.8);
}
#header2:active ~ #header4{
    background-color: rgba(10,10,50,0.8);
}

https://jsfiddle.net/ThinkingStiff/a3y52/ In this example I found whilist trying to find out the problem, it works fine, and I can't see any differences. https://jsfiddle.net/ThinkingStiff/a3y52/在这个例子中,我发现whilist试图找出问题,它工作正常,我看不出任何差异。 The elements aren't children or parents of eachother either. 这些元素也不是彼此的孩子或父母。 (And I'd like to fix this without Javascript/Jquery if possible) (如果可能的话,我想在没有Javascript / Jquery的情况下解决这个问题)

My HTML: 我的HTML:

<div id="header1"><a data-scroll id="link1" href="#header1content"></a></div>
<div id="header2"><a data-scroll id="link2" href="#header2content"></a></div>
<div id="header3"><a data-scroll id="link3" href="#header3content"></a></div>
<div id="header4"><a data-scroll id="link4" href="#header4content"></a></div>

In order to make this work, #header2 should be the first element in your markup. 为了使这项工作, #header2应该是标记中的第一个元素。 Because ~ selector in css can select elements which are preceded (Not followed) by a specific element. 因为css中的~选择器可以选择特定元素之前(未跟随)的元素。

Therefore If you wants to style different elements on hover of #header2 then #header2 should come first in markup. 因此,如果你想在#header2悬停时设置不同的元素,那么#header2应该在标记中#header2在第一位。 In the same way if you wants to style different elements on hover of #header4 then #header4 should come first in markup. 同样地,如果你想在#header4悬停时设置不同的元素,那么#header4应该在标记中#header4在第一位。

With css flex you can change order of elements. 使用css flex您可以更改元素的顺序。 It has an order property for child elements that can change order. 它有一个可以改变顺序的子元素的order属性。 You can make it work as shown below: 您可以使其工作如下所示:

 .headers { flex-direction: column; display: flex; } .headers div { background: #ccc; margin: 0 0 10px; height: 50px; } #header2 {order: 2;} #header1 {order: 1;} #header3 {order: 3;} #header4 {order: 4;} #header2:hover { background-color: rgba(10,50,10,0.9); } #header2:active { background-color: rgba(0,0,0,0); } #header2:active ~ #header1, #header2:hover ~ #header1 { background-color: rgba(0,0,0,0.8); } #header2:active ~ #header3, #header2:hover ~ #header3 { background-color: rgba(50,10,10,0.8); } #header2:active ~ #header4, #header2:hover ~ #header4 { background-color: rgba(10,10,50,0.8); } 
 <div class="headers"> <div id="header2"><a data-scroll id="link2" href="#header2content">Header 2</a></div> <div id="header1"><a data-scroll id="link1" href="#header1content">Header 1</a></div> <div id="header3"><a data-scroll id="link3" href="#header3content">Header 3</a></div> <div id="header4"><a data-scroll id="link4" href="#header4content">Header 4</a></div> </div> 

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

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