影响横幅div的多个div

时间:2019-03-20 15:49:33

标签: html css

我希望每当我将鼠标悬停在另一个div上时,横幅(“受影响的”)都会更改, 每个div都会在我的横幅上显示不同的效果(“受影响”)。

 <div class="hover-container">
  this is the hover container
<div class="hover-me">hover me</div>
<div class="square1">hover me</div>
<div class="square2">hover me</div>
<div class="square3">hover me</div>
<div class="square4">hover me</div>
<div class="square5">hover me</div>
<div class="square6">hover me</div>
<div class="square7">hover me</div>
<div class="square8">hover me</div>
<div class="square9">hover me</div>
</div>

<div class="affected">
  affected
</div>

我的CSS是

.hover-container:hover ~ .affected{
 background:pink;
}
   .square1:hover ~ .affected{
 background:blue;
}

...直到平方9 ... 然后

.hover-container{
  pointer-events:none;
}

.hover-me, .square1, .square2, .square3, .square4, .square5, .square6, 
.square7, .square8, .square9{
  pointer-events:auto;
  cursor:pointer;
}

1 个答案:

答案 0 :(得分:0)

我尝试了您的代码,它似乎正常工作。也许有帮助吗?

.square9:hover~.affected {
  background: blue;
}

.square8:hover~.affected {
  background: red;
}

.square8,
.square9 {
  background: #eeeeee;
}

div {
  float: left;
  width: 100px;
  height: 100px;
}

.hover-container {
  width: 100%;
  pointer-events: none;
}

.hover-me,
.square1,
.square2,
.square3,
.square4,
.square5,
.square6,
.square7,
.square8,
.square9 {
  pointer-events: auto;
  cursor: pointer;
}
<div class="hover-container">
  <div class="square8">hover me</div>
  <div class="square9">hover me</div>

  <div class="affected">
    affected
  </div>
</div>