【问题标题】:How to toggle the display property of two sibling elements on hover of one?如何在一个悬停时切换两个兄弟元素的显示属性?
【发布时间】:2020-05-30 10:28:10
【问题描述】:

当我将鼠标悬停在第一个兄弟元素上时,我试图切换两个兄弟元素的显示。我想将悬停的第一个兄弟元素的显示从块更改为无,而其直接或下一个兄弟元素的样式从无更改为块。我能够为此编写一个 css 代码,但是在悬停时,元素不断闪烁并在一个永无止境的循环中交替。下面是我的html和css代码:

.box {
  display: inline-block;
  width: 300px;
  height: 150px;
  margin: 20px 40px;
  border-radius: 4px;
  cursor: pointer;
}

.box-one {
  background: green;
}

.box-two {
  background: whitesmoke;
  display: none;
}

.box-one:hover {
  display: none;
}

.box-one:hover+.box-two {
  display: block;
}
<div class="box box-one"></div>
<div class="box box-two"></div>

【问题讨论】:

    标签: html css


    【解决方案1】:

    用另一个 div (.container) 包裹这两个框,并在悬停容器时切换它们:

    .box {
      display: inline-block;
      width: 300px;
      height: 150px;
      margin: 20px 40px;
      border-radius: 4px;
      cursor: pointer;
    }
    
    .box-one {
      background: green;
    }
    
    .box-two {
      background: whitesmoke;
    }
    
    .container {
      width: fit-content;
    }
    
    .container:hover>.box-one {
      display: none;
    }
    
    .container:not(:hover)>.box-two {
      display: none;
    }
    <div class="container">
      <div class="box box-one"></div>
      <div class="box box-two"></div>
    </div>

    使用 JS mouseentermouseleave 事件,您可以将带有 once: true 选项的事件监听器仅添加到当前项目。鼠标一进入,就显示新项目,只有当用户离开当前项目时,才将新的mouseenter事件监听器添加到当前项目,以此类推……

    const showNext = evt => {
      const current = evt.target
      const next = current.nextElementSibling
    
      if(!next || !next.classList.contains('box')) return
      
      current.classList.remove('show')
    
      next.classList.add('show')
      
      next.addEventListener('mouseleave', () => {
        next.addEventListener('mouseenter', showNext, { once: true })
      }, { once: true })
    }
    
    document.querySelector('.box')
      .addEventListener('mouseenter', showNext, { once: true })
    .box {
      display: none;
      width: 300px;
      height: 150px;
      margin: 20px 40px;
      border-radius: 4px;
      cursor: pointer;
    }
    
    .show {
      display: inline-block;
    }
    
    .box-one {
      background: green;
    }
    
    .box-two {
      background: whitesmoke;
    }
    
    .box-three {
      background: red;
    }
    
    .box-four {
      background: blue;
    }
    
    .container {
      width: fit-content;
    }
    <div class="box box-one show"></div>
    <div class="box box-two"></div>
    <div class="box box-three"></div>
    <div class="box box-four"></div>

    【讨论】:

    • 哦,是的。我尝试了这种方法,但我遇到的问题是,一旦光标在容器中,它就会改变容器中所有框的背景颜色,而不是直接在单个元素框上。当元素框超过两个时,这会更加成问题。我计划使用它更动态地创建元素框。
    • 您必须为每对创建一个容器。顺便说一句 - 你只是改变颜色吗?
    • 不,我不会单独更改颜色。这只是一个模型。我实际上是用另一个内容更多的盒子换掉它们。
    • 您是否要为每个内容替换一对以上的项目,或者您是否有 3 张或更多卡片可以相互替换?
    • 我将拥有不止一对。例如:用户可以决定创建 10 个盒子或他喜欢的任何数量。这意味着对于用户在 DOM 中创建的每个框,将自动创建 10 个额外框作为同级框。悬停任何框时,应显示动态创建的同级框元素。
    猜你喜欢
    • 2013-12-07
    • 1970-01-01
    • 2017-06-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-06-30
    • 1970-01-01
    相关资源
    最近更新 更多