前提:有时候在开发过程中需要动态渲染类名,比如高亮显示,active状态等。
下面是react写法,记录一下:
<div className={counts>0?"logo highlight":"logo"} >
<Icon className={counts>0?"icon-shopping-cart highlight":"icon-shopping-cart"} size="md" type="check-circle-o" />
</div>
这段代码的目的是当counts大于0的时候添加高亮状态。
另外scss对于这种active写法(实例 a标签的hover状态和普通标签active状态): &表示
引用父选择符:
1)hover写法
a {
font-weight: bold;
text-decoration: none;
&:hover { text-decoration: underline; }
body.firefox & { font-weight: normal; }
}
编译后
a {
font-weight: bold;
text-decoration: none; }
a:hover {
text-decoration: underline; }
body.firefox a {
font-weight: normal; }
2)active状态
.icon-shopping-cart{
color: #80858a;
width: 30px;
height: 30px;
margin-top: 6px;
&.highlight{
color: #fff;
}
}
编译后
.icon-shopping-cart{
color: #80858a;
width: 30px;
height: 30px;
margin-top: 6px;
}
.icon-shopping-cart .highlight{
color: #fff;
}