选择CSS中的上一条基准线-更新时间轴轴

时间:2019-04-04 06:14:44

标签: html css vertical-alignment timeline

我正在寻找一种CSS解决方案,用于将背景颜色应用于当前事件中上一个事件的垂直时间轴上

.timeline {
    position: relative;
    display: flex;
    margin: 10px;
    flex-direction: column;
}

.timeline .event {
    position: relative;
    display: flex;
    flex-direction: row;
    padding-left: 15px;
}

.timeline .event::before {
    content: '';
    position: absolute;
    top: 0px;
    left: 2px;
    bottom: 0px;
    width: 2px;
    background: #d9d9d9;
}

.timeline.descending .event.open + .event:nth-child(3n-1)::before {
    background: #555555;
} 
<div class="timeline descending">
    <div class="event">
        Event 1
    </div>
    <div class="event">
        Event 2
    </div>
    <div class="event open">
        Event 3
    </div>
    <div class="event">
        Event 4
    </div>
    <div class="event">
        Event 5
    </div>
    <div class="event">
        Event 6
    </div>
</div>

在上面的示例中,我为Event 3打开了一个类,我尝试使用Event 2更改#555555轴的颜色,我编写了--

.timeline.descending .event.open + .event:nth-child(3n-1)::before {
    background: #555555;
}

哪个没用!

但是,当我将代码修改为

.timeline.descending .event + .event:nth-child(3n-1)::before {
    background: #555555;
}

.timeline {
    position: relative;
    display: flex;
    margin: 10px;
    flex-direction: column;
}

.timeline .event {
    position: relative;
    display: flex;
    flex-direction: row;
    padding-left: 15px;
}

.timeline .event::before {
    content: '';
    position: absolute;
    top: 0px;
    left: 2px;
    bottom: 0px;
    width: 2px;
    background: #d9d9d9;
}

/* .timeline .event:nth-child(1)::before {
    background: #000000;
} */

.timeline.descending .event + .event:nth-child(3n-1)::before {
    background: #555555;
}
<div class="timeline descending">
    <div class="event">
        Event 1
    </div>
    <div class="event">
        Event 2
    </div>
    <div class="event open">
        Event 3
    </div>
    <div class="event">
        Event 4
    </div>
    <div class="event">
        Event 5
    </div>
    <div class="event">
        Event 6
    </div>
</div>

我能够运行它,它给了我部分结果。我对nth-childnth-last-child的工作了解有限,希望能对您有所帮助。

2 个答案:

答案 0 :(得分:0)

我不确定您是否可以选择前面的元素,但是如果您将问题抛在脑后,则可以选择下面的元素。

例如:

.timeline {
    position: relative;
    display: flex;
    margin: 10px;
    flex-direction: column;
}

.timeline .event {
    position: relative;
    display: flex;
    flex-direction: row;
    padding-left: 15px;
}

.timeline .event::before {
    content: '';
    position: absolute;
    top: 0px;
    left: 2px;
    bottom: 0px;
    width: 2px;
    background: #555;
}

.timeline.descending .event.open ~ .event::before {
    background: #d9d9d9;
}
<div class="timeline descending">
    <div class="event">
        Event 1
    </div>
    <div class="event">
        Event 2
    </div>
    <div class="event open">
        Event 3
    </div>
    <div class="event">
        Event 4
    </div>
    <div class="event">
        Event 5
    </div>
    <div class="event">
        Event 6
    </div>
</div>

答案 1 :(得分:0)

如果您只想更改(n)th索引(您可以递增地更改n,可以从第二个索引开始选择;对于第一个兄弟姐妹,可以使用{{1} })

.timeline.descending .event:first-child::before

在此示例中,您还可以将.timeline.descending .event:nth-child(1) + .event::before { background: #555555; } :nth-child(1)交换。

请注意,CSS中有no previous sibling selector

:first-child
.timeline {
    position: relative;
    display: flex;
    margin: 10px;
    flex-direction: column;
}

.timeline .event {
    position: relative;
    display: flex;
    flex-direction: row;
    padding-left: 15px;
}

.timeline .event::before {
    content: '';
    position: absolute;
    top: 0px;
    left: 2px;
    bottom: 0px;
    width: 2px;
    background: #d9d9d9;
}


.timeline.descending .event:nth-child(1) + .event::before {
    background: #555555;
}

或者,如果您使用JavaScript很好,则可以使用:

<div class="timeline descending">
    <div class="event">
        Event 1
    </div>
    <div class="event">
        Event 2
    </div>
    <div class="event open">
        Event 3
    </div>
    <div class="event">
        Event 4
    </div>
    <div class="event">
        Event 5
    </div>
    <div class="event">
        Event 6
    </div>
</div>

const evt = document.querySelectorAll('.event');
for(let i = 0; i < evt.length ; i++) {
    if(((i+1) != evt.length) && evt[i+1].classList.contains('open')){
      var styleElem = document.head.appendChild(document.createElement("style"));   
      styleElem.innerHTML = ".timeline.descending .event:nth-child("+ (i+1) + ")::before {background: #555555;}";
    }
}
const evt = document.querySelectorAll('.event');
for(let i = 0; i < evt.length ; i++) {
    if(((i+1) != evt.length) && evt[i+1].classList.contains('open')){
      var styleElem = document.head.appendChild(document.createElement("style"));
      styleElem.innerHTML = ".timeline.descending .event:nth-child("+ (i+1) + ")::before {background: #555555;}";
    }
}
.timeline {
    position: relative;
    display: flex;
    margin: 10px;
    flex-direction: column;
}

.timeline .event {
    position: relative;
    display: flex;
    flex-direction: row;
    padding-left: 15px;
}

.timeline .event::before {
    content: '';
    position: absolute;
    top: 0px;
    left: 2px;
    bottom: 0px;
    width: 2px;
    background: #d9d9d9;
}