【问题标题】:How to change colour of mdc-select dropdown arrow?如何更改 mdc-select 下拉箭头的颜色?
【发布时间】:2020-01-12 04:47:48
【问题描述】:

我正在尝试更改 mdc-select 中下拉箭头的默认紫色:

似乎没有一个 sass mixin 可以做到这一点。

它是怎么做的?

这是我迄今为止尝试过的:

HTML

<div class="mdc-select mdc-select--outlined">
    <input type="hidden" name="enhanced-select">
    <i class="mdc-select__dropdown-icon"></i>
    <div class="mdc-select__selected-text" class="mdc-select__selected-text" role="button" aria-haspopup="listbox" aria-labelledby="demo-label"></div>
    <div class="mdc-select__menu mdc-menu mdc-menu-surface">
        <ul class="mdc-list">
            <li class="mdc-list-item" data-value="41" role="option">41</li>
            <li class="mdc-list-item" data-value="42" role="option">42</li>
            <li class="mdc-list-item" data-value="43" role="option">43</li>
        </ul>
    </div>
    <div class="mdc-notched-outline">
        <div class="mdc-notched-outline__leading"></div>
        <div class="mdc-notched-outline__notch">
            <label class="mdc-floating-label">Answer to Life</label>
        </div>
        <div class="mdc-notched-outline__trailing"></div>
    </div>
</div>  

SCSS

@import "@material/list/mdc-list";
@import "@material/menu-surface/mdc-menu-surface";
@import "@material/menu/mdc-menu";
@import "@material/select/mdc-select";

.mdc-select {
    @include mdc-select-ink-color(red);
    // @include mdc-select-container-fill-color(red);
    @include mdc-select-label-color(red);
    @include mdc-select-focused-label-color(red);
    @include mdc-select-bottom-line-color(red);
    @include mdc-select-focused-bottom-line-color(red);
    @include mdc-select-hover-bottom-line-color(red);
    @include mdc-select-outline-color(red);
    @include mdc-select-focused-outline-color(red);
    @include mdc-select-hover-outline-color(red);
    @include mdc-select-icon-color(red);
}

TS

import { MDCSelect } from '@material/select';
const select = new MDCSelect(document.querySelector('.mdc-select'));

【问题讨论】:

    标签: css angular material-design mdc-components


    【解决方案1】:

    最简单最简单的方法是隐藏 SVG 并通过 CSS 创建自己的下拉箭头(无需更改 HTML)。然后,您可以根据需要轻松更改颜色:

    .mdc-select__dropdown-icon{
      background: none;
      width: 0;
      height: 0;
      bottom: 24px;
      right: 10px;  
      border-left: 5px solid transparent;
      border-right: 5px solid transparent;
      border-top: 5px solid #333333;
    }
    .mdc-select--activated .mdc-select__dropdown-icon,
    .mdc-select--focused .mdc-select__dropdown-icon {
      border-top: 5px solid #FF0000;
    }
    

    【讨论】:

      【解决方案2】:

      我设法通过使用 MDC 内部 Sass 混合来为其设置主题:mdc-select-dd-arrow-svg-bg_。一个例子是:

      .mdc-select .mdc-select__dropdown-icon {
          @include mdc-select-dd-arrow-svg-bg_(COLOR, 1);
      }
      

      其中COLOR 是MDC 主题属性(例如'secondary')或HTML 颜色(例如#ff0000)。它不能是 var,因为正如 @KuldipKoradia 所指出的,SVG 是在编译时生成的。

      【讨论】:

        【解决方案3】:

        这不是图标或字体,此箭头是.mdc-select--focused .mdc-select__dropdown-icon 类的背景图像。您可以使用filter css 属性更改图像颜色。

        filter: hue-rotate(60deg);
        

        将此 css 应用到您的自定义 css 类 .mdc-select--focused .mdc-select__dropdown-icon 它将起作用。

        谢谢。

        【讨论】:

        • 完美。谢谢!
        • 只是为了记录,以防其他人出现在这里寻找类似的东西,我只是用红色作为问题的例子,认为所有颜色都是一样的。我实际上想让下拉箭头变灰,我发现filter: grayscale(1); 对我有用。再次感谢。我不知道 CSS 过滤器。
        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2022-08-19
        • 2015-02-10
        • 2015-11-16
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2017-02-11
        相关资源
        最近更新 更多