css - Reverse states on hover with Less -
i have following less code:
.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'; } } }
with essential goal being there normal state , hover state, reversed when class present. i'll repeating other actions (eg .share-action-link
, .review-action-link
etc) , seems messy way have it. there way create mixin such provide so:
.favourite-action-link { &:after { color:@grey; content: '\e836'; &:hover { color:@red; content: '\e811'; } .reverseonclass(is-favourite); } }
or that? way can think of far do:
.favourite-action-link { &:after { color:@grey; content: '\e836'; } &.active:after { color:@red; content: '\e811'; } }
and use jquery instead hover - toggling .active
on (ishovering xor hasclass(is-favourite))
- turning less less + jquery opposite of fixing mess/maintainability issue.
i recommend writing below because keeps code simple , easy read.
.favourite-action-link { &:after, &.is-favourite:hover:after { color: @grey; content: '\e836'; } &:hover:after, &.is-favourite:after { color: @red; content: '\e811'; } }
but if want use mixin avoid repeating selectors write below. mixin takes 2 rulesets input , applied required selectors.
.favourite-action-link { .rules-gen( { color: @grey; content: '\e836'; }; { color: @red; content: '\e811'; } ); } .rules-gen(@rule1; @rule2){ &:after, &.is-favourite:hover:after { @rule1(); } &:hover:after, &.is-favourite:after { @rule2(); } }
in both these methods, selectors grouped , means reduced lines of code.
or, if class not is-favourite
, else pass mixin parameter below:
.favourite-action-link { .rules-gen( { color: grey; content: '\e836'; }; { color: red; content: '\e811'; }; ~"is-favourite" ); } .share-action-link { .rules-gen( { color: yellow; content: '\e836'; }; { color: gold; content: '\e811'; }; ~"active" ); } .rules-gen(@rule1; @rule2; @addedclass){ &:after, &.@{addedclass}:hover:after { @rule1(); } &:hover:after, &.@{addedclass}:after { @rule2(); } }
Comments
Post a Comment