ant design 树形控件a-tree-select展开子级选项只能点左侧小三角形,希望能点击title文字就能展开

文章介绍了如何通过CSS调整Antdesign中的tree选择器,增加伪元素扩大点击区域,使得用户可以点击标题文字来控制子级节点的展开和收起,同时通过hover效果提升用户体验。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

在这里插入图片描述
问题: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;
  }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值