【发布时间】:2016-02-08 05:19:06
【问题描述】:
我有以下 LESS 代码:
.favourite-action-link {
&:after {
color:@grey;
content: '\e836';
}
&.is-favourite:after {
color:@red;
content: '\e811';
}
&:hover {
&:after {
color:@red;
content: '\e811';
}
&.is-favourite:after {
color:@grey;
content: '\e836';
}
}
}
基本目标是存在正常状态和悬停状态,当存在另一个类时它们会反转。对于其他操作(例如.share-action-link、.review-action-link 等),我将重复此操作,而这看起来很混乱。有没有办法创建一个mixin,这样我就可以像这样提供:
.favourite-action-link {
&:after {
color:@grey;
content: '\e836';
&:hover {
color:@red;
content: '\e811';
}
.reverseOnClass(is-favourite);
}
}
或者类似的东西?到目前为止,我能想到的唯一方法是:
.favourite-action-link {
&:after {
color:@grey;
content: '\e836';
}
&.active:after {
color:@red;
content: '\e811';
}
}
然后使用 jQuery 代替悬停 - 在 (isHovering XOR hasClass(is-favourite)) 上切换 .active - 但将 LESS 变为 LESS + jQuery 与修复混乱/可维护性问题相反。
【问题讨论】:
标签: css less less-mixins