elementPlus的tree组件点击后有白色背景

在使用elementPlus的tree组件时,需要对它进行样式的重写,下面是相关代码

html 复制代码
<script setup>
import { ref } from 'vue'
const data = [
  {
    label: 'Level one 1',
    children: [
      {
        label: 'Level two 1-1',
        children: [
          {
            label: 'Level three 1-1-1'
          }
        ]
      }
    ]
  },
  {
    label: 'Level one 2',
    children: [
      {
        label: 'Level two 2-1',
        children: [
          {
            label: 'Level three 2-1-1'
          }
        ]
      },
      {
        label: 'Level two 2-2',
        children: [
          {
            label: 'Level three 2-2-1'
          }
        ]
      }
    ]
  },
  {
    label: 'Level one 3',
    children: [
      {
        label: 'Level two 3-1',
        children: [
          {
            label: 'Level three 3-1-1'
          }
        ]
      },
      {
        label: 'Level two 3-2',
        children: [
          {
            label: 'Level three 3-2-1'
          }
        ]
      }
    ]
  }
]
</script>

<template>
  <div class="tree_box">
    <el-tree style="max-width: 600px" :data="data" />
  </div>
</template>

<style scoped lang="scss">
.tree_box {
  background-color: purple;
  width: 400px;
  height: 400px;
  padding: 100px;
}
:deep(.el-tree) {
  background-color: transparent; // 整个tree组件的背景色
  .el-tree-node:focus > .el-tree-node__content {
    background: black !important; // tree选中之后的背景色
  }
  .el-tree-node__content {
    &:hover {
      background: black; // 鼠标hover背景色
    }
  }
  .el-tree-node__label {
    color: #fff; // 文字颜色
  }
}
</style>

下面是各个class的解释:

  • .el-tree:这个就是整个tree组件的根元素,这个class默认背景色是白色,看需求是变成透明还是什么颜色
  • .el-tree-node:focus > .el-tree-node__content:这个class会在点击tree之后生效,默认是白色
  • .el-tree-node__content:这个class是树组件的每一行的元素,鼠标hover时默认为白色背景,可以修改成需求的颜色
  • .el-tree-node__label: 这个class是树文字的class,可以设置文字的相关样式
相关推荐
OEC小胖胖1 小时前
去中心化身份:2025年Web3身份验证系统开发实践
前端·web3·去中心化·区块链
vvilkim2 小时前
Electron 进程间通信(IPC)深度优化指南
前端·javascript·electron
ai小鬼头4 小时前
百度秒搭发布:无代码编程如何让普通人轻松打造AI应用?
前端·后端·github
漂流瓶jz4 小时前
清除浮动/避开margin折叠:前端CSS中BFC的特点与限制
前端·css·面试
前端 贾公子4 小时前
在移动端使用 Tailwind CSS (uniapp)
前端·uni-app
散步去海边4 小时前
Cursor 进阶使用教程
前端·ai编程·cursor
清幽竹客4 小时前
vue-30(理解 Nuxt.js 目录结构)
前端·javascript·vue.js
知性的小mahua4 小时前
echarts+vue实现中国地图板块渲染+省市区跳转渲染
vue.js
weiweiweb8884 小时前
cesium加载Draco几何压缩数据
前端·javascript·vue.js
幼儿园技术家4 小时前
微信小店与微信小程序简单集成指南
前端