【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;
      }
    }
  }
}
相关推荐
zzzzzz310几秒前
36K stars 之外:如何阅读 react-bits 这样的动画交互式 React 项目
javascript·react.js·动效
ljt27249606612 分钟前
Flutter笔记--envied
前端·笔记·flutter
Flynt22 分钟前
React Router v8 升了之后,我花了两天才把项目跑起来
前端·typescript·preact
泯泷7 小时前
手搓JSVMM第 8 篇:函数调用:参数、返回值与调用帧
前端·javascript·前端框架
Sherotree7 小时前
Google Antigravity——Agent 被提到主界面
前端·ide·后端·edge浏览器
Mr数据杨7 小时前
【Codex】用学生入学评估模块建立新生能力画像
android·java·javascript·django·codex·项目开发
里欧跑得慢8 小时前
Flutter 像素级还原:设计稿到代码的视觉无损转换技巧
前端·css·flutter·web
里欧跑得慢8 小时前
Flutter 三方库 function_tree — 鸿蒙应用开发中的动态数学公式解析与计算神器,实现鸿蒙深度适配下的复杂函数逻辑运行时求值实战(适配鸿蒙 HarmonyOS Next ohos)
android·前端·安全·flutter·华为·harmonyos
IT毕设实战小研8 小时前
基于安卓的空气质量查询系统
android·大数据·vue.js·爬虫·python·django·课程设计
苏灿烤鱼9 小时前
没有机密文件,也没有付费数据,在浏览器里造一颗"间谍卫星"
前端·javascript·github