用details写一个纯HTML5的折叠菜单

技巧2年前 隔壁老李于 2022-06-22 10:33:26 最后编辑

details是HTML5新增的一个有趣又没用的标签,因为它兼容性实在有点差,按照百度来的说法,details只兼容Chrome, Safari 8+ ,Opera 26+

details标签用于描述文档或文档某个部分的细节,更容易理解的一种用法:折叠菜单

例子:

<details class="menu" open>
  <summary>菜单</summary>
  <ul>
    <li><a href="#">子菜单</a></li>
    <li><a href="#">子菜单</a></li>
  </ul>
</details>

open定义details是否可见,说白了就是加上就默认展开,不加就是默认关闭。

可以用css定义相关样式

.menu summary {
  height: 40px;
  line-height: 40px;
  text-indent: 10px;
  outline: none;
  font-weight: 700;
  border-top: 1px solid #ddd;
  background: -webkit-gradient(linear, left top, left bottom, color-stop(0, #FEFEFE), color-stop(1, #CCCCCC));
  cursor: pointer;
}
.menu ul {
  padding: 10px 0;
  margin: 0;
}
.menu ul li {
  list-style: none;
  text-indent: 25px;
  height: 30px;
  line-height: 30px;
}
.menu ul li a {
  display: block;
  color: #666;
  text-decoration: none;
}

完整代码:

<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>无标题文档</title>
<style>
* {
    margin: 0;
    padding: 0;
}
a {
    text-decoration: none;
}
#conter {
    width: 1000px;
    margin: auto;
}
#help-left {
    width: 200px;
    font-family: 'microsoft YaHei';
    float: left;
}
.menu {
    border-left: 1px solid #ccc;
    border-right: 1px solid #ccc;
}
.menu:last-child {
    border-bottom: 1px solid #ccc;
}
.menu summary {
    height: 40px;
    line-height: 40px;
    text-indent: 10px;
    outline: none;
    font-size: 14px;
    font-weight: 700;
    border-top: 1px solid #ddd;
    background: -webkit-gradient(linear, left top, left bottom, color-stop(0, #FEFEFE), color-stop(1, #CCCCCC));
    cursor: pointer;
}
.menu summary::-webkit-details-marker {
display: none;
}
/*可用图片或字符,如果使用图片,把content的值设置为空*/
.menu summary:before {
    content: "+";
	/*background: url(../Images/right.png) no-repeat center center;*/ /*收起时的图片*/
    display: inline-block;
    width: 16px;
    height: 16px;
    margin-right: 10px;
    font-size: 18px;
    font-weight: 700;
}
.menu[open] summary:before {
    content: "-";
	/*background: url(../Images/down.png) no-repeat center center;*/ /*展开时的图片*/
}
.menu ul {
    padding: 10px 0;
}
.menu ul li {
    list-style: none;
    text-indent: 25px;
    font-size: 12px;
    height: 30px;
    line-height: 30px;
}
.menu ul li a {
    display: block;
    color: #666;
}
.menu ul li a:hover {
    text-decoration: underline;
}
</style>
</head>

<body>
<section id="conter">
  <section id="help-left">
    <details class="menu" open>
      <summary>菜单1</summary>
      <ul>
        <li><a href="#">子菜单</a></li>
        <li><a href="#">子菜单</a></li>
      </ul>
    </details>
    <details class="menu" open>
      <summary>菜单2</summary>
      <ul>
        <li><a href="#">子菜单</a></li>
        <li><a href="#">子菜单</a></li>
      </ul>
    </details>
    <details class="menu" open>
      <summary>菜单3</summary>
      <ul>
        <li><a href="#">子菜单</a></li>
        <li><a href="#">子菜单</a></li>
        <li><a href="#">子菜单</a></li>
      </ul>
    </details>
    <details class="menu" open>
      <summary>菜单4</summary>
      <ul>
        <li><a href="#">子菜单</a></li>
        <li><a href="#">子菜单</a></li>
        <li><a href="#">子菜单</a></li>
      </ul>
    </details>
  </section>
</section>
</body>
</html>

不考虑兼容性的情况下还是挺好用的,毕竟现在的浏览器只有Chrome和其他~~

用details写一个纯HTML5的折叠菜单  第1张

本文由 @隔壁老李 于 2022-06-01 发布在 野路子博客,如无特别说明,本博文章均为原创,转载请保留出处。
评论 (0)
访客
Top