【Vue + Element Plus】el-tree树结构样式改造,添加转折线

1. 效果图

2. 代码实现

html 复制代码
<el-tree
  ref="treeRef"
  class="tree-box"
  :data="treeData"
  node-key="nodeId"
  :props="defaultProps"
  :current-node-key="currNodeId"
  highlight-current
  lazy
  :load="loadNode"
  :filter-node-method="filterNode"
  @node-click="handleNodeClick"
>
  <template #default="{ node, data }">
    <div class="custom-tree-node">
      <div class="tree-node-text">{{ node.label }}</div>
      <div class="tree-node-icons">
        <el-icon @click.stop="handleAddNode(data)" v-if="node.level === 2"><CirclePlus /></el-icon>
        <el-icon @click.stop="handleEditNode(data)" v-if="node.level === 3"><Edit /></el-icon>
        <el-icon @click.stop="handleDeleteNode(data)" v-if="node.level === 3"><Delete /></el-icon>
      </div>
    </div>
  </template>
</el-tree>
css 复制代码
// 重置el-tree默认样式
.tree-box {
	padding: 0 12px 12px;
  overflow: auto;
  border-radius: 0 0 4px 4px;
  
  .el-button {
    font-size: 16px;
  }
  .el-button + .el-button {
    margin-left: 8px;
  }
  // 树节点内容样式
  .el-tree-node__content {
    height: 100%;
    font-size: 16px;
    line-height: 28px; // 行高变化竖线位置也要相应调整
    color: #333;
    position: relative;
    padding-left: 0 !important;

    &:hover {
      background-color: transparent;
    }
  }
  .el-tree-node__children {
    padding-left: 24px;
  }
  // 树节点展开/折叠图标
  .el-tree-node__expand-icon {
    padding: 0;
    color: #979797;
  }

  // 添加转折线样式
  .el-tree-node {
    position: relative;
    background-color: transparent;

    // "非根节点" "非最后一个子节点" "整体" 前面加竖线,高度为整体的100%
    .el-tree-node__children > .el-tree-node:not(:last-child)::after {
      content: '';
      position: absolute;
      left: -18px;
      top: 0;
      width: 1px;
      height: 100%;
      background-color: #979797;
      z-index: 0;
    }
    // "非根节点" "最后一个子节点" "名称" 前面加竖线,高度为名称的50%
    .el-tree-node__children > .el-tree-node:last-child > .el-tree-node__content::after {
      content: '';
      position: absolute;
      left: -18px;
      top: 0;
      width: 1px;
      height: 50%;
      background-color: #979797;
      z-index: 0;
    }

    // 每个子节点左侧的横线 - 连接到父节点的竖线
    &.is-expanded .el-tree-node__children .el-tree-node__content::before {
      content: '';
      position: absolute;
      left: -18px; // 连接到竖线位置
      top: 50%; // 子节点中间
      width: 18px; // 横线长度
      height: 1px;
      background-color: #979797;
      z-index: 0;
    }

    // 叶子节点没有下箭头,横线宽度加一点
    &.is-leaf > .el-tree-node__content::before {
      width: 26px !important;
    }
  }

  // 当前选中节点高亮样式优化
  .el-tree-node.is-current > .el-tree-node__content {
    background-color: transparent;
    .tree-node-text {
      background-color: #00c3a5;
    }
  }

  .custom-tree-node {
    display: flex;
    align-items: center;
    justify-content: space-between;
    font-size: 16px;
    width: 100%;
    border-radius: 4px;
    margin: 4px 0;

    &:hover {
      background-color: #e5f8f5;
    }

    .tree-node-text {
      white-space: pre-wrap;
      word-break: break-all;
      border-radius: 4px;
      padding: 0 12px !important;
    }
    .tree-node-icons {
      margin-left: 8px;
      display: flex;
      align-items: center;
      font-size: 14px;

      .el-icon {
        font-size: 16px;
      }
      .el-icon + .el-icon {
        margin-left: 8px;
      }
      .el-button {
        font-size: 14px;
      }
    }
  }
}
相关推荐
前端小趴菜051 天前
vue3项目优化方案
前端·javascript·vue.js
Mr_Swilder1 天前
WebGPU 基础 (WebGPU Fundamentals)
前端
张3蜂1 天前
HTML5语义化标签:现代网页的骨架与灵魂
前端·html·html5
悟空瞎说1 天前
我用 PixiJS 撸了个圆桌会议选座系统,从 0 到 1 踩坑全复盘
前端
码云之上1 天前
从 SPA 到全栈:AI 时代的前端架构升级实践
前端·架构·ai编程
Irene19911 天前
对比总结:Vue3中的 watch 和 Pinia中的 $subscribe
vue.js·pinia·watch·subscribe
小陈同学呦1 天前
关于如何使用CI/CD做自动化部署
前端·后端
前端Ah1 天前
记 华为鸿蒙机型小程序使用uni.createInnerAudioContext() 播放音频播放两次的问题
前端
用户221765927921 天前
css border-left 怎么设置 border 展示为椭圆
前端
御形封灵1 天前
纯CSS实现方块下落等待动画
前端·css