简体   繁体   English

当 ion-tab-button 处于活动状态时,如何更改图标的颜色? CSS 离子

[英]How can i change the color of my icon when ion-tab-button is active? CSS Ionic

I am using Ionic 4.12 I am working with the tab component, and i want to change the color of my ion-icon svg when i activate that tab.我正在使用 Ionic 4.12 我正在使用选项卡组件,我想在激活该选项卡时更改我的 ion-icon svg 的颜色。 I'm trying to change the shadow dom of the ion-tab-button as the documentation shows with我正在尝试更改 ion-tab-button 的 shadow dom,如文档所示

--color-selected --颜色选择

--background-focused --以背景为中心

in the css but it does not change it在 css 中,但它不会改变它

tab bar code标签条码

<ion-tab-bar slot="bottom">
<ion-tab-button tab="mainview">
  <ion-icon src="assets/logo/mainView.svg"></ion-icon>
  <ion-label>INDICADORES</ion-label>
</ion-tab-button>

<ion-tab-button tab="profile">
  <ion-icon src="assets/logo/profile.svg"></ion-icon>
  <ion-label>PERFIL</ion-label>
</ion-tab-button>

<ion-tab-button tab="">
  <ion-icon src="assets/logo/phone.svg"></ion-icon>
  <ion-label>LLAMAR</ion-label>
</ion-tab-button>

<ion-tab-button tab="caregivers">
  <ion-icon src="assets/logo/doc.svg"></ion-icon>
  <ion-label>CUIDADORES</ion-label>
</ion-tab-button>

<ion-tab-button tab="help">
  <ion-icon src="assets/logo/help.svg"></ion-icon>
  <ion-label>AYUDA</ion-label>
</ion-tab-button>

current css of the icons图标的当前 css

ion-tab-button{
font-size: 10px;
--padding-end: 0px;
--padding-start: 10px;
--padding-bottom: 0px;
--margin-left:0px;
--margin-right:0px;
max-width:100px;
ion-icon{
    font-size: 67.5px;
}

If you want to give another color when a class is active you can simply do the following:如果您想在类处于活动状态时提供另一种颜色,您只需执行以下操作:

.class:active {
    color: blue; 
}

However in your case this would be:但是,在您的情况下,这将是:

ion-tab-button:active{
    color: blue;
}

The color atribute also works with hex and RGB codes (see https://www.w3schools.com/cssref/css_colors_legal.asp for more)颜色属性也适用于十六进制和 RGB 代码(有关更多信息,请参见https://www.w3schools.com/cssref/css_colors_legal.asp

I also reccomend to check this post out as this is related to the problem you are having at the moment.我还建议您查看这篇文章,因为这与您目前遇到的问题有关。 Editing Ionic tab icon styles 编辑 Ionic 选项卡图标样式

ion-tab-button{
font-size: 10px;
--background-focused: #a0a;
--color-selected: #a0a;
--padding-end: 0px;
--padding-start: 10px;
--padding-bottom: 0px;
--margin-left:0px;
--margin-right:0px;
max-width:100px;
ion-icon{
    font-size: 67.5px;
}}

在此处输入图片说明

this is the correct ionic way这是正确的离子方式

This is the below CSS, we need to add it in component level CSS if we need to apply the styles only to the specific page.这是下面的 CSS,如果我们只需要将样式应用于特定页面,我们需要将它添加到组件级 CSS。

Use of focus pseudo class, we can apply the style to the selected ion-tab-button使用focus伪类,我们可以将样式应用到选中的ion-tab-button

ion-tab-button:focus {
    ion-icon, ion-label {
        color: var(--ion-color-primary) !important;
        --ion-color-base: var(--ion-color-primary) !important;
    }
}

You can provide the custom css to selected tab.您可以为所选选项卡提供自定义 css。 Whenever you select the tab "tab-selected" class is added in ion-tab-button.每当您选择选项卡时,都会在 ion-tab-button 中添加“tab-selected”类。

.tab-selected {
  border-bottom: 5px solid blue;
}

There is one more way to customize your tabs in ionic.还有另一种方法可以在 ionic 中自定义选项卡。 (my ionic version 6.12.0) (我的离子版本 6.12.0)

ion-tab-bar{
    background-color: white;
    overflow-x: auto;
    border:0px;
}

ion-tab-bar > ion-tab-button {
 
  border-radius: 20px 20px 20px 20px;
}

ion-tab-button[aria-selected=true] {
    color:white;
    background-color: #3689ef;
 }

 
 ion-tab-bar  ion-tab-button[aria-selected=true]:first-child {   
    border-radius: 0px 20px 20px 0px;
 }
 ion-tab-bar  ion-tab-button[aria-selected=true]:last-child {
  
    border-radius: 20px 0px 0px 20px;
 }

This is much more difficult to figure out than it should be but here's what worked for me:这比它应该的更难弄清楚,但这是对我有用的:

In theme/variables.css

:root {

  /*put at the bottom after all other base styles*/
  --ion-tab-bar-color: #2a3042;
  --ion-tab-bar-color-selected: #556ee6;

}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM