html - 如何在分层树结构中制作方向箭头

标签 html css angular

堆栈:Angular 4 + HTML + CSS

我想制作分层树结构,其中父节点有一个指向子节点的箭头(见下图)。

enter image description here

单击父选项卡时会显示子项。我的功能运行良好,我已经能够实现以下目标: enter image description here

我使用的CSS如下:

.tree li::before {
  border-left: 1px solid #999;
  bottom: 50px;
  height: 100%;
  top: 0;
  width: 1px
}

.tree li::after {
  border-top: 1px solid #999;
  height: 20px;
  top: 25px;
  width: 25px
}

我想用以下定向箭头替换这些行。 如果有人创建了类似的结构,请指导我。

最佳答案

您可以使用伪元素实现此目的:

.tree__nested-items {
  list-style-type: none;
  margin: 0;
  padding: 0;
  padding-left: 70px;
  display: flex;
  flex-direction: column;
  align-items: flex-start;
}

.tree__item {
  width: 200px;
  height: 30px;
  line-height: 30px;
  border: 2px solid #dedede;
  background-color: #f7f8fa;
  border-radius: 7px;
  padding: 10px 25px;
  margin: 10px;
}

.tree__item--nested {
  position: relative;
}

/* line */
.tree__item--nested:before {
  content: "";
  position: absolute;
  bottom: 50%;
  /* padding of container - 10px */
  left: -60px;
  /* padding of container - 10px */
  width: 60px;
  /* 3 * half height + half of border radius */
  height: calc(150% + 7.5px);
  border-bottom: inherit;
  border-left: inherit;
  border-bottom-left-radius: 15px;
  /* don't overlap items */
  z-index: -1;
}

/* arrow */
.tree__item--nested:after {
  content: "";
  position: absolute;
  /* border-width - 1px */
  left: -8px;
  bottom: calc(50% - 6px);
  border-color: transparent;
  border-left-color: inherit;
  border-style: inherit;
  border-width: 7px;
}
<div class="tree__item">Some title</div>
<ul class="tree__nested-items">
  <li class="tree__item tree__item--nested">Some item one</li>
  <li class="tree__item tree__item--nested">Some item two</li>
  <li class="tree__item tree__item--nested">Third item</li>
</ul>

根据 OP 的要求,这里是用于更复杂结构的 CSS。我们在这里使用 libefore 作为 left 边框,使用 after 作为 bottom 边框圆 Angular 和 spanbefore 箭头三 Angular 形。

$(function() {
  $('.tree li:has(ul)').addClass('parent_li').find(' > span').attr('title', 'Collapse this branch');
  $('.tree li.parent_li > span').on('click', function(e) {
    var children = $(this).parent('li.parent_li').find(' > ul > li');
    if (children.is(":visible")) {
      children.hide('fast');
      $(this).attr('title', 'Expand this branch').find(' > i').addClass('icon-plus-sign').removeClass('icon-minus-sign');
    } else {
      children.show('fast');
      $(this).attr('title', 'Collapse this branch').find(' > i').addClass('icon-minus-sign').removeClass('icon-plus-sign');
    }
    e.stopPropagation();
  });
});
.tree {
  min-height: 20px;
  padding: 19px;
  margin-bottom: 20px;
  background-color: #fbfbfb;
  border: 1px solid #999;
  -webkit-border-radius: 4px;
  -moz-border-radius: 4px;
  border-radius: 4px;
  -webkit-box-shadow: inset 0 1px 1px rgba(0, 0, 0, 0.05);
  -moz-box-shadow: inset 0 1px 1px rgba(0, 0, 0, 0.05);
  box-shadow: inset 0 1px 1px rgba(0, 0, 0, 0.05)
}

.tree li {
  list-style-type: none;
  margin: 0;
  padding: 10px 5px 0 5px;
  position: relative
}

.tree li::before,
.tree li::after {
  content: '';
  left: -20px;
  position: absolute;
  right: auto
}

.tree li::before {
  border-left: 1px solid #999;
  bottom: 50px;
  height: 100%;
  top: 0;
}

.tree li::after {
  box-sizing: border-box;
  border-left: 1px solid #999;
  border-bottom: 1px solid #999;
  border-bottom-left-radius: 10px;
  height: 25px;
  top: 0px;
  width: 25px;
}

.tree li span {
  -moz-border-radius: 5px;
  -webkit-border-radius: 5px;
  border: 1px solid #999;
  border-radius: 5px;
  display: inline-block;
  padding: 3px 8px;
  text-decoration: none;
}

.tree li.parent_li > span {
  cursor: pointer;
}

.tree > ul > li::before,
.tree > ul > li::after {
  border: 0;
}

.tree li:last-child::before {
  height: 14px;
}

.tree li:not(.parent_li) span {
  position: relative;
}

.tree li span:before {
  content: "";
  position: absolute;
  /* border-width - 1px */
  left: -4px;
  bottom: calc(50% - 4px);
  border-color: transparent;
  border-left-color: #999;
  border-style: inherit;
  border-width: 4px;
}

/* another styles for parent_li immediate span children */
.tree li.parent_li > span:before {
  bottom: initial;
  left: 2px;
  top: 20.5px;
}

/* hide arrow for immediate children */
.tree > ul > li > span:before {
  display: none;
}

.tree li.parent_li > span:hover,
.tree li.parent_li > span:hover + ul li span {
  background: #eee;
  border: 1px solid #94a0b4;
  color: #000;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<link href="https://netdna.bootstrapcdn.com/twitter-bootstrap/2.3.1/css/bootstrap-combined.min.css" rel="stylesheet" />
<script src="https://netdna.bootstrapcdn.com/twitter-bootstrap/2.3.1/js/bootstrap.min.js"></script>

<div class="tree well">
  <ul>
    <li>
      <span><i class="icon-folder-open"></i> Parent</span> <a href="">Goes somewhere</a>
      <ul>
        <li>
          <span><i class="icon-minus-sign"></i> Child</span> <a href="">Goes somewhere</a>
          <ul>
            <li>
              <span><i class="icon-leaf"></i> Grand Child</span> <a href="">Goes somewhere</a>
            </li>
          </ul>
        </li>
        <li>
          <span><i class="icon-minus-sign"></i> Child</span> <a href="">Goes somewhere</a>
          <ul>
            <li>
              <span><i class="icon-leaf"></i> Grand Child</span> <a href="">Goes somewhere</a>
            </li>
            <li>
              <span><i class="icon-minus-sign"></i> Grand Child</span> <a href="">Goes somewhere</a>
              <ul>
                <li>
                  <span><i class="icon-minus-sign"></i> Great Grand Child</span> <a href="">Goes somewhere</a>
                  <ul>
                    <li>
                      <span><i class="icon-leaf"></i> Great great Grand Child</span> <a href="">Goes somewhere</a>
                    </li>
                    <li>
                      <span><i class="icon-leaf"></i> Great great Grand Child</span> <a href="">Goes somewhere</a>
                    </li>
                  </ul>
                </li>
                <li>
                  <span><i class="icon-leaf"></i> Great Grand Child</span> <a href="">Goes somewhere</a>
                </li>
                <li>
                  <span><i class="icon-leaf"></i> Great Grand Child</span> <a href="">Goes somewhere</a>
                </li>
              </ul>
            </li>
            <li>
              <span><i class="icon-leaf"></i> Grand Child</span> <a href="">Goes somewhere</a>
            </li>
          </ul>
        </li>
      </ul>
    </li>
    <li>
      <span><i class="icon-folder-open"></i> Parent2</span> <a href="">Goes somewhere</a>
      <ul>
        <li>
          <span><i class="icon-leaf"></i> Child</span> <a href="">Goes somewhere</a>
        </li>
      </ul>
    </li>
  </ul>
</div>

<div class="tree">
  <ul>
    <li>
      <span><i class="icon-calendar"></i> 2013, Week 2</span>
      <ul>
        <li>
          <span class="badge badge-success"><i class="icon-minus-sign"></i> Monday, January 7: 8.00 hours</span>
          <ul>
            <li>
              <a href=""><span><i class="icon-time"></i> 8.00</span> &ndash; Changed CSS to accomodate...</a>
            </li>
          </ul>
        </li>
        <li>
          <span class="badge badge-success"><i class="icon-minus-sign"></i> Tuesday, January 8: 8.00 hours</span>
          <ul>
            <li>
              <span><i class="icon-time"></i> 6.00</span> &ndash; <a href="">Altered code...</a>
            </li>
            <li>
              <span><i class="icon-time"></i> 2.00</span> &ndash; <a href="">Simplified our approach to...</a>
            </li>
          </ul>
        </li>
        <li>
          <span class="badge badge-warning"><i class="icon-minus-sign"></i> Wednesday, January 9: 6.00 hours</span>
          <ul>
            <li>
              <a href=""><span><i class="icon-time"></i> 3.00</span> &ndash; Fixed bug caused by...</a>
            </li>
            <li>
              <a href=""><span><i class="icon-time"></i> 3.00</span> &ndash; Comitting latest code to Git...</a>
            </li>
          </ul>
        </li>
        <li>
          <span class="badge badge-important"><i class="icon-minus-sign"></i> Wednesday, January 9: 4.00 hours</span>
          <ul>
            <li>
              <a href=""><span><i class="icon-time"></i> 2.00</span> &ndash; Create component that...</a>
            </li>
          </ul>
        </li>
      </ul>
    </li>
    <li>
      <span><i class="icon-calendar"></i> 2013, Week 3</span>
      <ul>
        <li>
          <span class="badge badge-success"><i class="icon-minus-sign"></i> Monday, January 14: 8.00 hours</span>
          <ul>
            <li>
              <span><i class="icon-time"></i> 7.75</span> &ndash; <a href="">Writing documentation...</a>
            </li>
            <li>
              <span><i class="icon-time"></i> 0.25</span> &ndash; <a href="">Reverting code back to...</a>
            </li>
          </ul>
        </li>
      </ul>
    </li>
  </ul>
</div>

关于html - 如何在分层树结构中制作方向箭头,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45207412/

相关文章:

php - 从表单中选择日期范围,然后从选定范围的 MySQL 数据中导出 CSV

html - 如何以响应方式定位 6 个 Bootstrap 容器?

asp.net - 隐藏错误但保留 ASP.NET Validation summary 的 validation summary header

javascript - Angular 6 :services with providedIn: 'root' returns empty object in component

angular - 如何以 Angular 4 路由

javascript - 如何从一个组件发布事件并从nativescript中的另一个组件接收事件

jquery - 将表单数据保存到本地存储并在刷新时显示

html - 我如何设置选择输入右侧的配置文件图像输入标签而不是 "choose file"->'choose image'

javascript - 将当前范围编号直接附加到 HTML 范围 slider 拇指上方

css - 使用 Meteor 和 Reactjs 控制加载哪些 CSS