ant design vue选择框背景色更改

需求        

        因为要实现网页白天模式和夜间模式的切换,那相应文字和组件颜色也要适配对应的主题,而a-select默认的白色背景色在夜间模式下非常突兀,故我们要修改选择框和选项列表框的颜色。

效果

        白天模式

        夜间模式

实现

        先介绍下背景,本次使用的是vue3框架,另外关于下面代码中的getPopupContainer这个API,是因为a-select默认相对于body定位的,故我们点开选项列表后滑动页面,选项列表会相对视口固定,即与选项框分离,使用getPopupContainer将a-select绑定到他的父节点上,这样就解决了两者分离问题:

	<a-select ref="select" v-model:value="optionValue" class="select" :class="props.myClass"
				:getPopupContainer="triggerNode => { return triggerNode.parentNode }">
				<template v-for="(item, index) in props.options" :key="index">
					<a-select-option :value="item.value">{{ item.name }}</a-select-option>
				</template>
			</a-select>

        修改a-select的样式,我们需要进行样式穿透,按需求我们要修改选项框字体、背景、下拉箭头颜色,选项列表字体、背景颜色,选中后的选项颜色与悬停时选项颜色:

<style scoped lang='scss'>
.select {
		position: absolute;
		top: 0;
		right: 16px;
		width: 98px;
		height: 24px;
		font-size: 14px;

		&.moon {
			color: #fff;

			:deep() {
				.ant-select-arrow {
					color: #fff;
				}

				.ant-select-selector {
					height: 24px;
					border-radius: 4px;
					background-color: transparent;
				}

				.ant-select-dropdown {
					background-color: transparent;

					.ant-select-item {
						color: #fff;

						&:hover {
							background-color: #4a4a4a;
						}
					}

					.ant-select-item-option-active {
						background-color: #3a3a3a;
					}

					.ant-select-item-option-selected {
						background-color: #3a3a3a;
					}
				}
			}
		}
	}
</style>

实现效果

  • 3
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Leviash

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值