首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >随机设置<Avatar> backgroundColor

随机设置<Avatar> backgroundColor
EN

Stack Overflow用户
提问于 2019-03-19 22:59:46
回答 3查看 3.3K关注 0票数 1

我在样式主题中定义了三个backgroundColor。

代码语言:javascript
复制
avatar: {
    backgroundColor: red[500],
},
orangeAvatar: {
    margin: 10,
    color: '#fff',
    backgroundColor: deepOrange[500],
},
purpleAvatar: {
    margin: 10,
    color: '#fff',
    backgroundColor: deepPurple[500],
}, 

当阿凡达被加载时,我想随机选择其中的一个。

代码语言:javascript
复制
<Card>
            <CardHeader
                avatar={
                    <Avatar id="av" aria-label="Recipe"
                        className={classes.avatar}>{this.props.userName.charAt(0).toLocaleUpperCase()}
                    </Avatar>}
                title={this.props.userName} disableTypography={true}/>
            <CardActionArea disabled={this.state.images.length == 1 ? true : false}>
                <CardMedia
                    id={this.props.ownerId}
                    className={classes.media}
                    image={this.state.images[this.state.imageIndex]}
                    onClick={this.handleOnClick}
                />
            </CardActionArea>
        </Card>

有什么建议怎么做吗?

谢谢

EN

回答 3

Stack Overflow用户

回答已采纳

发布于 2019-03-19 23:04:26

有几种方法可以做你想做的事情。我的建议是:将这3个类放入一个数组中,每次选择一个介于0和2之间的随机数,并指定该类名:

<Avatar className={classes[Math.floor(Math.random() * 3)]}.../>

票数 2
EN

Stack Overflow用户

发布于 2019-03-19 23:09:06

代码语言:javascript
复制
let classNameHolder = ["avatar","orangeAvatar","purpleAvatar"];
<Card>
            <CardHeader
                avatar={
                    <Avatar id="av" aria-label="Recipe"
                        className={classNameHolder[Math.floor(Math.random() * 3)]}>{this.props.userName.charAt(0).toLocaleUpperCase()}
                    </Avatar>}
                title={this.props.userName} disableTypography={true}/>
            <CardActionArea disabled={this.state.images.length == 1 ? true : false}>
                <CardMedia
                    id={this.props.ownerId}
                    className={classes.media}
                    image={this.state.images[this.state.imageIndex]}
                    onClick={this.handleOnClick}
                />
            </CardActionArea>
        </Card>
票数 0
EN

Stack Overflow用户

发布于 2020-02-15 01:10:07

我也有同样的需求,也许这个解决方案也会为你服务,有一个随机生成颜色的函数,然后从在线样式中调用该函数。

代码语言:javascript
复制
const useStyles = makeStyles((theme: Theme) =>
  createStyles({
    large: {
      fontSize: "2.5rem",
      width: 100,
      height: 100
    }
  })
);

function randomColor() {
  let hex = Math.floor(Math.random() * 0xFFFFFF);
  let color = "#" + hex.toString(16);

  return color;
}

..。

代码语言:javascript
复制
  return (
    <Avatar
      variant="square"
      src={imageSrc}
      alt={alt}
      className={classes.large}
      style={{
        backgroundColor: randomColor()
      }}
    />
  )

参考:

Javascript random color

Avatar random backgroundColor on fallback

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

https://stackoverflow.com/questions/55243989

复制
相关文章

相似问题

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