问题:ant design 中tree选择器只能通过点击小三角展开子级,体验不太好,希望能点title文字展开/收起。
解决:
.ant-select-tree-switcher {
position: relative;
}
.ant-select-tree-switcher_open::before,
.ant-select-tree-switcher_close::before {
content: '';
position: absolute;
right: -200px;
top: 0;
left: 0;
bottom: 0;
}
.ant-select-tree-switcher_close:hover ~ .ant-select-tree-node-content-wrapper-close {
background-color: #e6f7ff;
transition: background-color 0.3s ease;
}
.ant-select-tree-switcher_open:hover ~ .ant-select-tree-node-content-wrapper {
background-color: #e6f7ff;
transition: background-color 0.3s ease;
}
通过小三角元素加一个伪元素让小三角点击范围覆盖右边文字部分,从而点击文字部分控制子级展开/收起,实际还是在操作小三角。另外通过hover来实现文字部分的背景,达到好的体验效果。
如果需要深度选择就在选择器前加/deep/,如下:
/deep/ .ant-select-tree-switcher {
position: relative;
}
/deep/ .ant-select-tree-switcher_open::before {
content: '';
position: absolute;
right: -200px;
top: 0;
left: 0;
bottom: 0;
}
/deep/ .ant-select-tree-switcher_close::before {
content: '';
position: absolute;
right: -200px;
top: 0;
left: 0;
bottom: 0;
}
/deep/ .ant-select-tree-switcher_close:hover ~ .ant-select-tree-node-content-wrapper-close {
background-color: #E6F7FF;
transition: background-color 0.3s ease;
}
/deep/.ant-select-tree-switcher_open:hover ~ .ant-select-tree-node-content-wrapper {
background-color: #E6F7FF;
transition: background-color 0.3s ease;
}