duanbushi1479 2016-05-22 09:30
浏览 70
已采纳

在循环中将另一个DIV悬停时显示DIV

i am trying to show a div when hovering another div. I researched this forum for solutions but i did not find anything that matched my problem, because mine is in a loop from 0 to 9. I know i just missed something really basic, i just cannot find it.

The whole code is PHP, the HTML lines are made with 'echo'. I want .runestext$i to show up when .runes$i is hovered. So, for example, when .runes1 is hovered i want to show up .runestext1 ^^ Here is my code:

<?php>
for ($i = 0; $i <= 9; $i++) {

//FROM HERE

if ($i >= 5) {
    $top_position = 493;
}
else {
    $top_position = 133;
}

if ($i >= 5) {
    $r = 18 + 250 * $i - 250 * 5 - 612.5;
}
else {
    $r = 18 + 250 * $i - 612.5;
}

$runes = getRunes($summoner_currentgame_specified);


echo "<style> " . ".runes" . $i . "{" . 
       "position: absolute;
        left: " . $r . "px;" . 
       "top: " . $top_position . "px;
        font-size: 16px;
        color: #fff;
        font-family: Verdana, Friz Quadrata Thin, sans-serif;
        font-weight:bold;" . 
     "</style>";

echo "<div id='a' class=runes" . $i . ">Runes</div>";

echo "<style> " . ".runes" . $i . ":hover" . "{" .
       "position: absolute;
        background-color: rgba(0, 0, 0, 0.9);
        width: 225px;
        height: 300px;
        margin-left: -21px;
        margin-top: -273px;
        text-align: center;
        border: 1px solid #fff;
        -webkit-border-radius: 5px;
        -moz-border-radius: 5px;
        border-radius: 25px;" . "}" .
     "</style>";

//TO HERE ALL IS WORKING FINE, just for clarity of the code

echo "<style>" . ".runestext" . $i . "{
        font-family: Verdana;
        font-style: normal;
        font-weight: bold;
        font-size: 16px;
        color: #fff;
        text-decoration: none;
        display: none; }" . 
      "</style>";

echo "<div id='b' class=runestext" . $i . ">RUNESTEXT</div>";

//Here my mistake should be, i just cannot find it:    
echo "<style>" . ".runes" . $i . ":hover .runestext" . $i . "{" .
       "display: block;" . "}" . 
     "</style>";
}
</php> 

I really hope you guys can help me as i dont get this working after a lot of time of trying.

  • 写回答

1条回答 默认 最新

  • duanlanqian9974 2016-05-22 09:42
    关注

    It appears your runestext is not a child of runes, which means you need the sibling selector ~ to target it.

    // ------------------------------------- ↓
    echo "<style>" . ".runes" . $i . ":hover ~ .runestext" . $i . "{" .
           "display: block;" . "}" . 
         "</style>";
    }
    

    Side notes

    1. You also miss a rule closure } in this style

      echo "<style> " . ".runes" . $i . "{" . 
            "position: absolute;
              left: " . $r . "px;" . 
            "top: " . $top_position . "px;
             font-size: 16px;
             color: #fff;
             font-family: Verdana, Friz Quadrata Thin, sans-serif;
             font-weight:bold;" . 
           "</style>";
      
    2. Don't use the same id on your div's, it should be unique

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 微信小程序协议怎么写
  • ¥15 c语言怎么用printf(“\b \b”)与getch()实现黑框里写入与删除?
  • ¥20 怎么用dlib库的算法识别小麦病虫害
  • ¥15 华为ensp模拟器中S5700交换机在配置过程中老是反复重启
  • ¥15 java写代码遇到问题,求帮助
  • ¥15 uniapp uview http 如何实现统一的请求异常信息提示?
  • ¥15 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看