如何通过具有特定类名的元素::before正确地将属性添加到块子元素?:last-child
此处的代码示例:
<div class="cv-viewer">
<div class="v-panel v-has-height"></div>
<div class="v-panel v-has-height"></div>
<div class="v-panel hidden"></div>
<div class="v-panel hidden"></div>
</div>
.cv-viewer {
.v-panel {
width: 300px;
height: 200px;
background-color: #000;
margin-bottom: 4px;
position: relative;
}
.hidden{
display: none;
}
& > .v-panel.v-has-height {
&::before {
content: '';
position: absolute;
left: 49px;
top: -32px;
width: 2px;
height: 100%;
background-color: #AD8CED;
z-index: 0;
}
}
& > .v-panel.v-has-height :last-child {
&::before {
content: '';
position: absolute;
left: 49px;
top: -32px;
width: 2px;
height: 58px;
background-color: #AD8CED;
z-index: 0;
}
}
}
由于标记是动态的,子元素的数量会发生变化,因此具有伪元素类的最后一个元素v-has-height必须具有::before有限的高度。
你需要的东西不能用纯 CSS 实现。
:last-child指的是父元素的最后一个元素,:last-of-type指的是某种类型的最后一个元素,或者换句话说,一个标签。因此,要么您需要对元素进行包装<div class="v-panel v-has-height"></div>,然后您可以使用 访问最后一个元素:last-child,要么您需要 JS: