css技巧系列之(四) 模拟树形结构

96 阅读1分钟

实现效果:

tree.gif 实现源码:

CSS部分:

  .tree summary {
        outline: 0;
        padding-left: 30px;
        list-style: none;
        background: repeating-linear-gradient(
            90deg,
            #999 0 1px,
            transparent 0px 2px
          )
          0px 50%/20px 1px no-repeat;
        /* background: linear-gradient(#999,#999) 0px 50%/20px 1px no-repeat; */
      }
      .tree details:last-child {
        background-size: 1px 23px;
      }
      .tree > details:not(:last-child) > details:last-child {
        background-size: 1px 100%;
      }
      .tree details {
        padding-left: 40px;
        background: repeating-linear-gradient(#999 0 1px, transparent 0px 2px)
          40px 0px/1px 100% no-repeat;
        /* background: linear-gradient(#999, #999) 40px 0px/1px 100% no-repeat; */
      }
      .tree > details {
        background: none;
        padding-left: 0;
      }
      .tree > details > summary {
        background: none;
      }
      .tree summary {
        display: flex;
        align-items: center;
        height: 46px;
        font-size: 15px;
        line-height: 22px;
        color: rgba(0, 0, 0, 0.85);
        cursor: default;
      }
      .tree summary::after {
        content: "";
        position: absolute;
        left: 10px;
        right: 10px;
        height: 38px;
        background: #eef2ff;
        border-radius: 8px;
        z-index: -1;
        opacity: 0;
        transition: 0.2s;
      }
      .tree summary:hover::after {
        opacity: 1;
      }
      .tree summary:not(:only-child)::before {
        content: "";
        width: 14px;
        height: 14px;
        flex-shrink: 0;
        margin-right: 8px;
        border: 1px solid #999;
        background: linear-gradient(#999, #999) 50%/1px 10px no-repeat,
          linear-gradient(#999, #999) 50%/10px 1px no-repeat;
      }
      .tree details[open] > summary::before {
        background: linear-gradient(#999, #999) 50%/10px 1px no-repeat;
      }

HTML部分

   <div class="tree">
      <details>
        <summary>目录-</summary>
        <!--没有内容了-->
        <details>
          <summary>1-1</summary>
        </details>
        <details>
          <summary>1-2</summary>
        </details>
        <details>
          <summary>1-3</summary>
        </details>
      </details>
      <details>
        <summary>目录二</summary>
        <details>
          <summary>2-1</summary>
          <details>
            <summary>2-1-1</summary>
            <details>
              <summary>2-1-1-1</summary>
            </details>
          </details>
          <details>
            <summary>2-1-2</summary>
          </details>
          <details>
            <summary>2-1-3</summary>
          </details>
        </details>
        <details>
          <summary>2-2</summary>
        </details>
        <details>
          <summary>2-3</summary>
        </details>
      </details>
      <details>
        <summary>目录五</summary>
        <!--没有内容了-->
      </details>
    </div>
    ```
    
    如果这些效果对你有所帮助,麻烦点个关注,以下为本人公众号
    
![公众号.jpg](https://p3-juejin.byteimg.com/tos-cn-i-k3u1fbpfcp/1c63e6f01e1643f69e822f60ddd56255~tplv-k3u1fbpfcp-watermark.image?)