[英]CSS / Less / Sass - Match every previous siblings when :hover
In this code: 在这段代码中:
<div id="Container">
<span class='first'>First</span>
<span class='second'>Second</span>
<span class='third'>Third</span>
</div>
I want to change color, when :hover
. 我希望在以下情况下改变颜色:hover
。
.first:hover
) THEN .first { color: red; }
IF( .first:hover
)THEN .first { color: red; }
.first { color: red; }
.second:hover
) THEN .first, .second { color: red; }
IF( .second:hover
)THEN .first, .second { color: red; }
.first, .second { color: red; }
.third:hover
) THEN .first, .second, .third { color: red; }
IF( .third:hover
)那么.first, .second, .third { color: red; }
.first, .second, .third { color: red; }
Is this possible without JS? 没有JS,这可能吗? I can accept CSS Hacks :) 我可以接受CSS Hacks :)
Possible answers: 可能答案:
More difficult version: 更难的版本:
<div id="Container">
<span class='first' style='color: red'>First</span>
<span class='second' style='color: green'>Second</span>
<span class='third' style='color: blue'>Third</span>
</div>
.first:hover
) THEN .first { color: pink; }
IF( .first:hover
)THEN .first { color: pink; }
.first { color: pink; }
.second:hover
) THEN .first, .second { color: pink; }
IF( .second:hover
)THEN .first, .second { color: pink; }
.first, .second { color: pink; }
.third:hover
) THEN .first, .second, .third { color: pink; }
IF( .third:hover
)那么.first, .second, .third { color: pink; }
.first, .second, .third { color: pink; }
Answers: 回答:
In CSS there is no previous sibling selector, but... you can sometimes do it using known selectors. 在CSS中没有先前的兄弟选择器,但是......有时你可以使用已知的选择器来完成它。 Sometimes. 有时。
<style>
span {color: #000; display: block;}
div:hover span {color: #f00;}
span:hover ~ span {color: #000}
</style>
<div id="FirstSecondThird-Container">
<span class='first'>First</span>
<span class='second'>Second</span>
<span class='third'>Third</span>
</div>
https://jsfiddle.net/45zLdcvr/ https://jsfiddle.net/45zLdcvr/
It works with block
span
s (of floated, of course). 它适用于block
span
(当然是浮动的)。 With span
s has default display: inline
it will blink when you will hover div
in space between spans. 使用span
s具有默认display: inline
它将在跨度之间将div
悬停在空格中时闪烁。
UPDATE : 更新 :
You updated the question when each span has own color, then it could be: 你更新了每个跨度有自己颜色的问题,然后它可能是:
span {color: red}
.second {color: green}
.third {color: blue}
span {display: block;}
div:hover span {color: pink;}
span:hover ~ .second {color: green}
span:hover ~ .third {color: blue}
https://jsfiddle.net/45zLdcvr/1/ https://jsfiddle.net/45zLdcvr/1/
I figured out a way with @each
in SASS based in panther's answer : 我在黑豹的答案中找到了与SASS中的@each
的方法:
$containerID: FirstSecondThird-Container;
##{$containerID}:hover span {color:pink}
@each $spanclass, $spancolor in (first, red), (second, green), (third, blue) {
##{$containerID} span:hover ~ .#{$spanclass}, .#{$spanclass} {
color: #{$spancolor};
}
}
It's a little bit shorter but here's the result (generated CSS): 它有点短,但这是结果(生成的CSS):
#FirstSecondThird-Container:hover span { color: pink; } #FirstSecondThird-Container span:hover ~ .first, .first { color: red; } #FirstSecondThird-Container span:hover ~ .second, .second { color: green; } #FirstSecondThird-Container span:hover ~ .third, .third { color: blue; } span {display: block;} /* this line was not generated, it's just to place them vertically */
<div id="FirstSecondThird-Container"> <span class='first'>First</span> <span class='second'>Second</span> <span class='third'>Third</span> </div>
Which has similar CSS rules to panther's fiddle . 对于黑豹的小提琴有类似的CSS规则。
Nice challenge 123qwe :) 好挑战123qwe :)
This is what i think can do what you're looking to achieve neatly 这就是我认为可以做到你想要实现的整洁
@mixin color_on_hover($class){
@if $class==first {span:nth-of-type(1){color:red;}}
@else if $class==second {span:nth-of-type(1), span:nth-of-type(2){color:red;}}
@else if $class==third {span:nth-of-type(1), span:nth-of-type(2), span:nth-of-type(3){color:red;}}
}
span.first:hover{
@include color_on_hover(first);
}
span.second:hover{
@include color_on_hover(second);
}
span.third:hover{
@include color_on_hover(third);
}
Hope it helps ! 希望能帮助到你 !
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.