聚焦时更改 mat-select-arrow 和 mat-select-underline [英] Change mat-select-arrow and mat-select-underline when focused

查看:22
本文介绍了聚焦时更改 mat-select-arrow 和 mat-select-underline的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

到目前为止,我已经尝试了很多不同的东西,例如:

So far I've tried lots of different things, such as:

/deep/ .mat-select:focus .mat-select-trigger .mat-select-arrow {
    color: #63961C;
}

/deep/ .mat-select:focus .mat-select-trigger .mat-select-underline {
    background-color: #63961C;
}

或者:

/deep/ .mat-select.mat-focused .mat-select-trigger .mat-select-arrow {
    color: #63961C;
}

/deep/ .mat-select.mat-focused .mat-select-trigger .mat-select-underline {
    background-color: #63961C;
}

更改选择旁边的小箭头和下划线.

to change that little arrow next to a select, and the underline.

例如,我做了

/deep/ .mat-input-container.mat-focused .mat-input-underline {
    background-color: #63961C;
}

用于 Input 的下划线,效果很好(聚焦时变为绿色).(是的,/deep/适用于这个项目,但如果我记得清楚,它现在已被弃用)

for the underline of an Input, and it worked fine (it becomes green when focusing). (yes /deep/ works fine for this project, though it's deprecated now if I remember well)

我设法一直"更改它,但我想要的是仅在聚焦时使其变为绿色,并未聚焦时保持灰色.

I managed to change it "all the time", but what I want, is to have it green only on focus, and keep it grey if not focused.

推荐答案

避免使用 /deep/(阅读此文档).您应该使用 ViewEncapsulation.

Avoid using /deep/ (read this documentation). You should use ViewEncapsulation.

在您的 ts 文件中,将 ViewEncapsulation 设置为无:

In your ts file, set ViewEncapsulation to None:

import { ViewEncapsulation } from '@angular/core';

@Component({
  ...
  encapsulation: ViewEncapsulation.None
})

.. 并将以下类添加到组件的 css 文件中:

.. and add the following classes to your component's css file:

/* to change arrow when focused */
.mat-select:focus:not(.mat-select-disabled).mat-primary .mat-select-arrow {
    color: #63961C;
}

/* to change underline when focused */
.mat-select:focus:not(.mat-select-disabled).mat-primary .mat-select-underline {
    background-color: #63961C;
}

/* to change plceholder text when focused */
.mat-select:focus:not(.mat-select-disabled).mat-primary .mat-select-trigger {
    color: #63961C;
}

/* to change selected item color in the select list */
.mat-primary .mat-option.mat-selected:not(.mat-option-disabled) {
    color: #63961C;
}

链接到工作演示.

为了使 css 更短,

To make the css shorter,

.mat-select:focus:not(.mat-select-disabled).mat-primary 
.mat-select-arrow , .mat-select-underline , .mat-select-trigger 
{
    color: #63961C;
}

/* to change selected item color in the select list */
.mat-primary .mat-option.mat-selected:not(.mat-option-disabled) {
    color: #63961C;
}

这篇关于聚焦时更改 mat-select-arrow 和 mat-select-underline的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆