代码之家  ›  专栏  ›  技术社区  ›  M.H Mighani

使用css将悬停应用于具有相同类名的所有元素

  •  -1
  • M.H Mighani  · 技术社区  · 7 年前

    悬停 所以当我把鼠标悬停在左右矩形的每一边 全部的 矩形的两侧位于屏幕顶部,html和css代码如下所示:

    <!DOCTYPE html>
    <html>
    <head>
        <link rel="stylesheet" type="text/css" href="style.css">
    </head>
    <body>
    <div id='container'>
        <div class='right'></div>
        <div class='right'></div>
        <div class='right'></div>
        <div class='right'></div>
        <div class='left'></div> 
        <div class='left'></div> 
        <div class='left'></div> 
        <div class='left'></div> 
    </div>
    </body>
    </html>
    

    css代码:

    body{
        margin: 0;
    }
    
    #container{
        position: relative;
        background: #D5D8DC;
        height:400px;
        width: 400px;
        margin-top: 100px;
        margin-left: 100px;
    }
    
    .right{
        background: #2ECC71;
    }
    
    .right:nth-child(1){
        position: absolute;
        height: 80%;
        width: 10%;
        top: 0;
        right: 0;
    }
    
    .right:nth-child(2){
        position: absolute;
        height: 10%;
        width: 80%;
        top: 0;
        right: 0;
    }
    
    .right:nth-child(3){
        position: absolute;
        height: 80%;
        width: 10%;
        top: 0;
        left: 20%;
        z-index: 10;
    }
    
    .right:nth-child(4){
        position: absolute;
        height: 10%;
        width: 80%;
        right: 0;
        bottom: 20%;
    }
    
    .left{
        background: #E74C3C;
    }
    
    .left:nth-child(5){
        position: absolute;
        height: 10%;
        width: 80%;
        bottom: 0;
        left: 0;
    }
    
    .left:nth-child(6){
        position: absolute;
        height: 80%;
        width: 10%;
        bottom: 0;
        left: 0;
    }
    
    .left:nth-child(7){
        position: absolute;
        height: 10%;
        width: 80%;
        left: 0;
        top: 20%;
    }
    
    .left:nth-child(8){
        position: absolute;
        height: 80%;
        width: 10%;
        bottom: 0;
        right: 20%;
    }
    
    .right:hover{
        z-index: 10;
    }   
    

    .right:hover{z-index:10} 这并不适用于所有人 正确的 侧边,它将z索引应用于鼠标悬停在其上的一侧。。。

    注: 我没有使用javascript或更改html代码的权限,我应该通过编写css代码来解决这个问题

    2 回复  |  直到 7 年前
        1
  •  2
  •   Paulie_D    7 年前

    根据您的HTML结构,您可以使用同级选择器和直接选择器进行管理,例如:

    .right:hover ~ .right,
    .right:hover {
      z-index: 10;
    }
    

    body {
      margin: 0;
    }
    
    #container {
      position: relative;
      background: #D5D8DC;
      height: 100px;
      width: 100px;
      margin-top: 10px;
      margin-left: 10px;
    }
    
    .right {
      background: #2ECC71;
    }
    
    .right:nth-child(1) {
      position: absolute;
      height: 80%;
      width: 10%;
      top: 0;
      right: 0;
    }
    
    .right:nth-child(2) {
      position: absolute;
      height: 10%;
      width: 80%;
      top: 0;
      right: 0;
    }
    
    .right:nth-child(3) {
      position: absolute;
      height: 80%;
      width: 10%;
      top: 0;
      left: 20%;
      z-index: 10;
    }
    
    .right:nth-child(4) {
      position: absolute;
      height: 10%;
      width: 80%;
      right: 0;
      bottom: 20%;
    }
    
    .left {
      background: #E74C3C;
    }
    
    .left:nth-child(5) {
      position: absolute;
      height: 10%;
      width: 80%;
      bottom: 0;
      left: 0;
    }
    
    .left:nth-child(6) {
      position: absolute;
      height: 80%;
      width: 10%;
      bottom: 0;
      left: 0;
    }
    
    .left:nth-child(7) {
      position: absolute;
      height: 10%;
      width: 80%;
      left: 0;
      top: 20%;
    }
    
    .left:nth-child(8) {
      position: absolute;
      height: 80%;
      width: 10%;
      bottom: 0;
      right: 20%;
    }
    
    .right:hover~.right,
    .right:hover {
      z-index: 10;
    }
    
    .left:hover~.left,
    .left:hover {
      z-index: 10;
    }
    <div id='container'>
      <div class='right'></div>
      <div class='right'></div>
      <div class='right'></div>
      <div class='right'></div>
      <div class='left'></div>
      <div class='left'></div>
      <div class='left'></div>
      <div class='left'></div>
    </div>
        2
  •  1
  •   Rainbow    7 年前

    基于可以使用的其他图元选择其他图元的步骤 ~ + .

    ~ 是阅读所有css的最佳方法 HERE

    现在那些选择器只向下选择,我的意思是,如果你把鼠标悬停在最后一个元素上,上面的元素不会受到影响,因为它只选择前面的元素。

    这是一个有效的演示

    body {
      margin: 0;
    }
    
    #container {
      position: relative;
      background: #D5D8DC;
      height: 400px;
      width: 400px;
    }
    
    .right {
      background: #2ECC71;
    }
    
    .right:nth-child(1) {
      position: absolute;
      height: 80%;
      width: 10%;
      top: 0;
      right: 0;
    }
    
    .right:nth-child(2) {
      position: absolute;
      height: 10%;
      width: 80%;
      top: 0;
      right: 0;
    }
    
    .right:nth-child(3) {
      position: absolute;
      height: 80%;
      width: 10%;
      top: 0;
      left: 20%;
      z-index: 10;
    }
    
    .right:nth-child(4) {
      position: absolute;
      height: 10%;
      width: 80%;
      right: 0;
      bottom: 20%;
    }
    
    .left {
      background: #E74C3C;
    }
    
    .left:nth-child(5) {
      position: absolute;
      height: 10%;
      width: 80%;
      bottom: 0;
      left: 0;
    }
    
    .left:nth-child(6) {
      position: absolute;
      height: 80%;
      width: 10%;
      bottom: 0;
      left: 0;
    }
    
    .left:nth-child(7) {
      position: absolute;
      height: 80%;
      width: 10%;
      right: 20%;
      top: 20%;
    }
    
    .left:nth-child(8) {
      position: absolute;
      height: 10%;
      width: 80%;
      top: 20%;
    }
    
    .right:hover ~ .right,
    .right:hover {
      z-index: 10;
    }
    
    .left:hover ~ .left,
    .left:hover {
      z-index: 10;
    }
    <div id='container'>
      <div class='right'></div>
      <div class='right'></div>
      <div class='right'></div>
      <div class='right'></div>
      <div class='left'></div>
      <div class='left'></div>
      <div class='left'></div>
      <div class='left'></div>
    </div>