網站首頁 編程語言 正文
el-tree默認的focus樣式顏色太淺,有時候電腦亮度低或者換個有色差的屏幕,根本看不出來哪一個節點被選中了。而且只有焦點在el-tree時才有顏色變化,鼠標在別的地方點一下就沒了,這樣會讓用戶忘記自己之前選的是哪個節點,很不方便。
第一步:給el-tree組件標簽加上屬性highlight-current開啟高亮
加了這個屬性,選中的節點的樣式才會有highlight-current類,這樣接下來才能改變選中的節點的樣式。
第二步:在css中修改高亮樣式
一個小tip:這里建議是給該頁面文件最外層的div加個專有的類,比如我這個頁面是“組織配置”,標簽就加個class=“organization_configuration”,然后style里的樣式全部寫在.organization_configuration{}中,這樣就不用加scoped了,也更符合vue組件化的開發思路
<style lang="less">
.organization_configuration {
.el-tree--highlight-current
.el-tree-node.is-current
> .el-tree-node__content {
// 設置顏色
background-color: rgba(135, 206, 235, 0.2); // 透明度為0.2的skyblue,作者比較喜歡的顏色
color: #409eff; // 節點的字體顏色
font-weight: bold; // 字體加粗
}
來看一下頁面效果:
如果想要的效果只是點擊時高亮,失去焦點后變回原樣的樣式,就不用給標簽加highlight-current屬性了,直接修改css即可:
.el-tree-node:focus > .el-tree-node__content {
background-color: rgba(135, 206, 235, 0.3);
color: #409eff; //節點的字體顏色
font-weight: bold;
}
指定默認高亮樹節點,參考:點擊鏈接
使用el-tree組件的setCurrentKey方法,根據樹組件的樹節點的唯一id來制定某個樹節點高亮。當然要搭配node-key="id"給樹節點綁定唯一標識id,同時也要開啟高亮模式(加上highlight-current屬性),然后方式一設置高亮的顏色樣式要加上。初始化加載默認高亮,所以在mounted鉤子中書寫代碼即可。
完整代碼:
<template>
<div class="box">
<el-tree
ref="myTree"
node-key="id"
:data="data"
:props="defaultProps"
highlight-current
>
</el-tree>
</div>
</template>
<script>
export default {
data() {
return {
data: [
{
name: "西游記",
id: "xiyouji",
children: [
{
name: "孫悟空",
id: "sunwukong",
children: [
{
name: "大猴子",
id: "dahouzi",
children: [],
},
{
name: "二猴子",
id: "erhouzi",
children: [],
},
],
},
{
name: "豬八戒",
id: "zhubajie",
children: [],
},
{
name: "沙和尚",
id: "shaheshang",
children: [],
},
],
},
{
name: "水滸傳",
id: "shuihuzhuan",
children: [
{
name: "宋江",
id: "songjiang",
children: [],
},
{
name: "武松",
id: "wusong",
children: [],
},
],
},
],
defaultProps: {
children: "children",
label: "name",
},
};
},
mounted() {
this.$nextTick(function () {
this.$nextTick(() => {
// myTree 數組件的ref 默認讓第一項高亮
// data是樹組件對應的數據
// 通過data中的第一項的id找到對應的節點,然后把這個節點設置為高亮
this.$refs.myTree.setCurrentKey(this.data[0].id);
});
});
},
};
</script>
<style lang="less" scoped>
// 設置高亮顏色
/deep/ .el-tree--highlight-current .el-tree-node.is-current > .el-tree-node__content {
background-color: #baf !important;
}
</style>
setCurrentKey方法是通過 key 設置某個節點的當前選中狀態,使用此方法必須設置 node-key 屬性,因為要確定唯一性,node-key="id"因為一般都是id具有唯一性,所以綁定id。
THX~
原文鏈接:https://blog.csdn.net/vvv3171071/article/details/124421368
- 上一篇:沒有了
- 下一篇:沒有了
相關推薦
- 2022-03-22 nginx中一個請求的count計數跟蹤淺析_nginx
- 2022-11-21 Qt實現小功能之復雜抽屜效果詳解_C 語言
- 2022-10-12 docker配置阿里云鏡像倉庫的實現_docker
- 2021-12-12 Docker?Consul概述以及集群環境搭建步驟(圖文詳解)_docker
- 2023-05-31 如何在ubuntu中切換使用不同版本的python_python
- 2022-05-02 關于Nginx中虛擬主機的一些冷門知識小結_nginx
- 2022-07-09 python沒有gpu,如何改用cpu跑代碼_python
- 2022-06-22 c++分離講解模板的概念與使用_C 語言
- 欄目分類
-
- 最近更新
-
- window11 系統安裝 yarn
- 超詳細win安裝深度學習環境2025年最新版(
- Linux 中運行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎操作-- 運算符,流程控制 Flo
- 1. Int 和Integer 的區別,Jav
- spring @retryable不生效的一種
- Spring Security之認證信息的處理
- Spring Security之認證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權
- redisson分布式鎖中waittime的設
- maven:解決release錯誤:Artif
- restTemplate使用總結
- Spring Security之安全異常處理
- MybatisPlus優雅實現加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務發現-Nac
- Spring Security之基于HttpR
- Redis 底層數據結構-簡單動態字符串(SD
- arthas操作spring被代理目標對象命令
- Spring中的單例模式應用詳解
- 聊聊消息隊列,發送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠程分支