列表在鼠标悬停时展开

时间:2015-04-07 19:23:04

标签: javascript jquery html css

我似乎无法找到任何显示如何扩展列表的信息。我希望它是一个水平类型的导航菜单,每个列表都有一个图像,当你点击图像时,它会展开“span”文本。我希望这是有道理的,并且我没有忽略其他人的问题。

示例:

<ul id="Topics">
    <li class="Items">
        <img src="results.png" alt="Results">
        <span>Title here</span>
        <span>Some more text here</span>
    </li>
    <li class="Items">
        <img src="language.png" alt="Language here">
        <span>Title here</span>
        <span>Some more text here</span>
    </li>
    <li class="Items">
        <img src="wording.png" alt="Wording here">
        <span>Title here</span>
        <span>Some more text here</span>
    </li>
</ul>

1 个答案:

答案 0 :(得分:2)

这是一个相当奇特的例子:

&#13;
&#13;
ul {
  text-align: left;
  display: inline;
  margin: 0;
  padding: 15px 4px 17px 0;
  list-style: none;
  -webkit-box-shadow: 0 0 5px rgba(0, 0, 0, 0.15);
  box-shadow: 0 0 5px rgba(0, 0, 0, 0.15);
}
ul li {
  font: bold 12px/18px sans-serif;
  display: inline-block;
  margin-right: -4px;
  position: relative;
  padding: 15px 20px;
  background: #fff;
  cursor: pointer;
}
ul li:hover {
  background: #555;
  color: #fff;
}
ul li ul {
  padding: 0;
  position: absolute;
  top: 48px;
  left: 0;
  width: 150px;
  -webkit-box-shadow: none;
  box-shadow: none;
  display: none;
  opacity: 0;
  visibility: hidden;
}
ul li ul li { 
  background: #555; 
  display: block; 
  color: #fff;
  text-shadow: 0 -1px 0 #000;
}
ul li ul li:hover { background: #666; }
ul li:hover ul {
  display: block;
  opacity: 1;
  visibility: visible;
}
&#13;
<ul id="Topics">
 <li class="Items">
    Title here
    <ul>
      <li>some more text here</li>
      <li>some more text here</li>
    </ul>
</li>
<li class="Items">
    Title here
    <ul>
      <li>some more text here</li>
      <li>some more text here</li>
    </ul>
</li>
<li class="Items">
    Title here
    <ul>
      <li>some more text here</li>
      <li>some more text here</li>
    </ul>
</li>
</ul>
&#13;
&#13;
&#13;

这里的关键代码是这一部分:

ul li:hover ul{
  display: block;
  opacity: 1;
  visibility: visible;
}

基本上,我们在悬停的ul内选择嵌套的li。这是有效的,因为:hover psudo-class与ul后代相结合,确保我们仅在其父li悬停时选择此列表。