首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何创建带有用户首字母的圆形彩色头像?

问如何创建带有用户首字母的圆形彩色头像?
EN

Stack Overflow用户
提问于 2022-04-24 19:11:29
回答 2查看 606关注 0票数 -1

我正在使用Ionic角应用程序开发。用用户的首字母创建圆形彩色头像的最佳方法是什么,例如联系人列表。就像这样:

​

​

提前感谢!干杯

EN

回答 2

Stack Overflow用户

发布于 2022-04-25 07:30:49

在.ts文件中创建一个函数:

代码语言:javascript
复制
 getInitials(firstName:string, lastName:string) {
    return firstName[0].toUpperCase() + lastName[0].toUpperCase();
  }

在getInitials文件中调用.html函数:

代码语言:javascript
复制
<div class="container">
  <div class="avatar"> {{getInitials('john','doe')}}</div>
  <br>
  <div class="avatar"> {{getInitials('john','doe')}}</div>
  <br>
  <div class="avatar"> {{getInitials('john','doe')}}</div>
  <br>
  <div class="avatar"> {{getInitials('john','doe')}}</div>
  <br>
  <div class="avatar"> {{getInitials('john','doe')}}</div>  
</div>

并将此片段添加到您的.scss文件中:

代码语言:javascript
复制
$colors: #a2b9bc,#6b5b95,#feb236, #d64161, #ff7b25, #b2ad7f,  #878f99;

.avatar {
    color: #fff; 
    padding: 2px;
    display: flex;
    justify-content: center;
    align-items: center;
    border-radius:100%;
    width:50px;
    height: 50px;
    font-weight: 600;
  }

  @for $i from 1 through length($colors) {
    .avatar:nth-child(#{length($colors)}n+#{$i}) {
        background: lighten(nth($colors, $i), 20%);
    }
}

结果如下:

​

​

票数 1
EN

Stack Overflow用户

发布于 2022-04-25 07:03:43

只有css,例如。

代码语言:javascript
复制
.avatar{
  width:3rem;        //<--use the size you choose
  border-radius:100%;
  text-align: center;
  background-color: pink;
}
.avatar span{
  line-height: 3rem; //<--see that it's the same size than the width
  color:red;
}

<div class="avatar">
  <span>AB</span>
</div>

您还可以定义一个“化身”数组。

代码语言:javascript
复制
  avatars=[{label:"AB",background:"LightGreen",color:"forestgreen"},
           {label:"WD",background:"lightsteelblue",color:"steelblue"}]

写

代码语言:javascript
复制
<div *ngFor="let avatar of avatars" class="avatar" 
        [style.background-color]="avatar.background">
  <span [style.color]="avatar.color">{{avatar.label}}</span>
</div>

见简单堆栈闪电战

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/71991566

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档