網(wǎng)站首頁 編程語言 正文
el-tree默認的focus樣式顏色太淺,有時候電腦亮度低或者換個有色差的屏幕,根本看不出來哪一個節(jié)點被選中了。而且只有焦點在el-tree時才有顏色變化,鼠標在別的地方點一下就沒了,這樣會讓用戶忘記自己之前選的是哪個節(jié)點,很不方便。
第一步:給el-tree組件標簽加上屬性highlight-current開啟高亮
加了這個屬性,選中的節(jié)點的樣式才會有highlight-current類,這樣接下來才能改變選中的節(jié)點的樣式。
第二步:在css中修改高亮樣式
一個小tip:這里建議是給該頁面文件最外層的div加個專有的類,比如我這個頁面是“組織配置”,標簽就加個class=“organization_configuration”,然后style里的樣式全部寫在.organization_configuration{}中,這樣就不用加scoped了,也更符合vue組件化的開發(fā)思路
<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; // 節(jié)點的字體顏色
font-weight: bold; // 字體加粗
}
來看一下頁面效果:
如果想要的效果只是點擊時高亮,失去焦點后變回原樣的樣式,就不用給標簽加highlight-current屬性了,直接修改css即可:
.el-tree-node:focus > .el-tree-node__content {
background-color: rgba(135, 206, 235, 0.3);
color: #409eff; //節(jié)點的字體顏色
font-weight: bold;
}
指定默認高亮樹節(jié)點,參考:點擊鏈接
使用el-tree組件的setCurrentKey方法,根據(jù)樹組件的樹節(jié)點的唯一id來制定某個樹節(jié)點高亮。當然要搭配node-key="id"給樹節(jié)點綁定唯一標識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 數(shù)組件的ref 默認讓第一項高亮
// data是樹組件對應的數(shù)據(jù)
// 通過data中的第一項的id找到對應的節(jié)點,然后把這個節(jié)點設置為高亮
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 設置某個節(jié)點的當前選中狀態(tài),使用此方法必須設置 node-key 屬性,因為要確定唯一性,node-key="id"因為一般都是id具有唯一性,所以綁定id。
THX~
原文鏈接:https://blog.csdn.net/vvv3171071/article/details/124421368
- 上一篇:沒有了
- 下一篇:沒有了
相關推薦
- 2022-10-10 AOSP源碼下載示例代碼_Android
- 2022-07-28 C++超詳細講解強制類型轉(zhuǎn)換_C 語言
- 2024-04-05 idea使用docker生成鏡像(打包鏡像,導入鏡像,導出鏡像)
- 2022-08-06 Python結合spaCy?進行簡易自然語言處理_python
- 2022-11-12 一文搞懂Go語言中defer關鍵字的使用_Golang
- 2023-10-14 C/C++ 批量梯度下降法實現(xiàn)一元線性回歸
- 2023-05-06 pandas中groupby操作實現(xiàn)_python
- 2022-07-18 linux系統(tǒng)安全和應用
- 欄目分類
-
- 最近更新
-
- window11 系統(tǒng)安裝 yarn
- 超詳細win安裝深度學習環(huán)境2025年最新版(
- Linux 中運行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎操作-- 運算符,流程控制 Flo
- 1. Int 和Integer 的區(qū)別,Jav
- spring @retryable不生效的一種
- Spring Security之認證信息的處理
- Spring Security之認證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權
- redisson分布式鎖中waittime的設
- maven:解決release錯誤:Artif
- restTemplate使用總結
- Spring Security之安全異常處理
- MybatisPlus優(yōu)雅實現(xiàn)加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務發(fā)現(xiàn)-Nac
- Spring Security之基于HttpR
- Redis 底層數(shù)據(jù)結構-簡單動態(tài)字符串(SD
- arthas操作spring被代理目標對象命令
- Spring中的單例模式應用詳解
- 聊聊消息隊列,發(fā)送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠程分支