简体   繁体   English

离子3条件下的颜色变化

[英]color change on condition in ionic 3

I want to change the color based on a condition. 我想根据条件更改颜色。 I display the n number of cards the card background must change based on the condition. 我显示n张卡片的数量,卡片背景必须根据条件进行更改。

 users.map((item)=>{ if(item.userType == "private"){ document.documentElement.style.setProperty('--customcolor',"yellow"); } else if(item.userType == "private"){ document.documentElement.style.setProperty('--customcolor',"blue"); } }); 
  :root { --customcolor: red; } .oddmes { background-color: var(--customcolor); } 
 <ion-list><br/> <ion-card class="oddmes" *ngFor="let user of users"> <ion-card-content > <b>{{user.user_name}} </b> <p>{{user.feedback}}</p> </ion-card-content> </ion-card> </ion-list> 

it will update the color which satisfy the condition at the end of the loop 它将在循环结束时更新满足条件的颜色

thanks in advance 提前致谢

Simply use : 只需使用:

<div [color]="item.userType == 'private' ? 'primary' : 'secondary'"> 
Dynamic color 
</div> 
  1. NgStyle directive lets you set a given DOM elements style properties. NgStyle指令使您可以设置给定的DOM元素样式属性。

    [ngStyle]="{'background-color':user.userType === 'private' ? 'green' : 'red' }"

  2. NgClass directive allows you to set the CSS class dynamically for a DOM element. NgClass指令允许您为DOM元素动态设置CSS类。

    [ngClass]="{'text-success':user.userType === 'private'}"

  3. Also you can create your own custom function like 您也可以创建自己的自定义函数,例如

    [ngStyle]="{'color':getColor(user.userType)}"

Javascript变色<div>有条件</div><div id="text_translate"><p>所以我有这个代码块,我需要 div 的颜色在紫色和白色之间“闪烁”,以实现条件“MECHANIC ON LINE”。 我知道在 switch 语句之外我正在实现的代码正常工作,但在 switch 语句中我不确定。 这也是在公司服务器上,我不确定后端的某些进程如何处理页面的更新。 无论如何,如果有人知道我在 switch 语句中实现我的代码是否不正确,请告诉我。 此外,第一个代码片段取自下面更大的片段,它是我关心的 switch 语句的片段。 最后,我还想提一下我已经将时间间隔设置为彼此不同的整数,例如“setInterval(function, 10 * 1000), setInterval(function, 20 * 2000)”。</p><pre> case "MECHANIC ON LINE": colorTitle[i].style.color = 'rgb(127.5, 127.5, 127.5)'; if(colorBackground[l].style.background === 'rgb(123,31,162)') { setInterval(colorBackground[l].style.background = 'rgb(255,255,255)',10 * 1000); } else { setInterval(colorBackground[l].style.background = 'rgb(123,31,162)',10 * 1000); } break;</pre><pre> for(var i = 0; i &lt; colorTitle.length; i++) { for(var l = i; l &lt; colorBackground.length; l++) { switch(colorTitle[i].textContent) { case "RUNNING": colorTitle[i].style.color = 'rgb(0, 255, 0)'; colorBackground[l].style.background = 'rgb(0, 255, 0)'; break; case "LINE DOWN": colorTitle[i].style.color = 'rgb(255, 0, 0)'; colorBackground[l].style.background = 'rgb(255, 0, 0)'; break; case "OFFLINE": colorTitle[i].style.color = 'rgb(127.5, 127.5, 127.5)'; colorBackground[l].style.background = 'rgb(127.5, 127.5, 127.5)'; break; case "MECHANIC ON LINE": colorTitle[i].style.color = 'rgb(127.5, 127.5, 127.5)'; if(colorBackground[l].style.background === 'rgb(123,31,162)') { setInterval(colorBackground[l].style.background = 'rgb(255,255,255)',10 * 1000); } else { setInterval(colorBackground[l].style.background = 'rgb(123,31,162)',10 * 1000); } break; default: colorTitle[i].style.color = 'rgb(255, 255, 255)'; colorBackground[l].style.background = 'rgb(255, 255, 255)'; break; } } }</pre></div> - Javascript color change <div> on condition

暂无
暂无

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

相关问题 jQuery更改颜色条件 - Jquery change color condition 更改徽章的颜色 - 条件 - change color of the badge - condition 根据值更改 div 颜色 - Change div color with condition on value 如何根据情况更改背景颜色 - How to change background color on condition Javascript变色<div>有条件</div><div id="text_translate"><p>所以我有这个代码块,我需要 div 的颜色在紫色和白色之间“闪烁”,以实现条件“MECHANIC ON LINE”。 我知道在 switch 语句之外我正在实现的代码正常工作,但在 switch 语句中我不确定。 这也是在公司服务器上,我不确定后端的某些进程如何处理页面的更新。 无论如何,如果有人知道我在 switch 语句中实现我的代码是否不正确,请告诉我。 此外,第一个代码片段取自下面更大的片段,它是我关心的 switch 语句的片段。 最后,我还想提一下我已经将时间间隔设置为彼此不同的整数,例如“setInterval(function, 10 * 1000), setInterval(function, 20 * 2000)”。</p><pre> case "MECHANIC ON LINE": colorTitle[i].style.color = 'rgb(127.5, 127.5, 127.5)'; if(colorBackground[l].style.background === 'rgb(123,31,162)') { setInterval(colorBackground[l].style.background = 'rgb(255,255,255)',10 * 1000); } else { setInterval(colorBackground[l].style.background = 'rgb(123,31,162)',10 * 1000); } break;</pre><pre> for(var i = 0; i &lt; colorTitle.length; i++) { for(var l = i; l &lt; colorBackground.length; l++) { switch(colorTitle[i].textContent) { case "RUNNING": colorTitle[i].style.color = 'rgb(0, 255, 0)'; colorBackground[l].style.background = 'rgb(0, 255, 0)'; break; case "LINE DOWN": colorTitle[i].style.color = 'rgb(255, 0, 0)'; colorBackground[l].style.background = 'rgb(255, 0, 0)'; break; case "OFFLINE": colorTitle[i].style.color = 'rgb(127.5, 127.5, 127.5)'; colorBackground[l].style.background = 'rgb(127.5, 127.5, 127.5)'; break; case "MECHANIC ON LINE": colorTitle[i].style.color = 'rgb(127.5, 127.5, 127.5)'; if(colorBackground[l].style.background === 'rgb(123,31,162)') { setInterval(colorBackground[l].style.background = 'rgb(255,255,255)',10 * 1000); } else { setInterval(colorBackground[l].style.background = 'rgb(123,31,162)',10 * 1000); } break; default: colorTitle[i].style.color = 'rgb(255, 255, 255)'; colorBackground[l].style.background = 'rgb(255, 255, 255)'; break; } } }</pre></div> - Javascript color change <div> on condition 根据条件更改单元格颜色 - change cell color based on condition Angular 按键颜色随条件变化 - Angular Button Color Change with Condition 根据条件更改父div的颜色 - Change color of parent div's color on a condition 离子更改javascript中的进度条颜色 - ionic change progressBar bar color in javascript 更改ionic.app.scss中的颜色 - Change color in ionic.app.scss
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM