【发布时间】:2021-10-01 03:50:34
【问题描述】:
我的 Scss 文件中有以下变量:
$flat-blue: #42a5f5;
$flat-green: #26a69a;
$flat-orange: #ffa727;
$flat-red: #ef5350;
$flat-purple: #c561d6;
我发现了一个 CodePen here,它可以在两个数字之间改变一个随机值,而且它似乎工作得很好。
有没有办法使用这个函数随机选择我的一个变量?
我的 CSS:
.avatar {
width: 43px;
height: 43px;
border-radius: 50%;
overflow: hidden;
z-index: 1;
position: relative;
background: $flat-green; //here should be the result of this function
float: left;
text-align: center;
line-height: 43px;
font-size: 20px;
color:white;
font-weight: lighter;
text-transform: uppercase;
}
我的 HTML 结构:
<article data-value="<%= subscriber[:id] %>"
data-status="<%= subscriber[:status] %>"
data-user="<%= subscriber[:subscriber][:user_id] %>"
data-profile="<%= subscriber[:profile_id] %>">
<div class="bg">
<div class="avatar">
<%= avatar_url(subscriber.profile.user.avatar, subscriber[:subscriber][:name]) %>
</div>
</div>
</article>
【问题讨论】:
-
将所有这些颜色放入单个数组列表中,然后将 random() 函数与 nth() 一起使用不是更容易吗?
-
@clive 我该怎么做?
-
@Harry 我该怎么做?
-
@vbotio: 类似this?
-
阅读文档,试一试,如果/当你遇到问题时,用代码更新问题
标签: javascript html css sass