我正在做一个时间轴,每个日期或日期范围都有一行,用条形图来表示时间跨度。我想我会得到最早日期和最晚日期之间的天数,然后计算每个日期栏的宽度和左边距或百分比(所以总时间的x%是宽度,开始日期和最早日期之间的时间除以总时间就是左边距百分比)。
我还想在所有其他行的顶部设置一个垂直条,显示当前日期。但我最终的结果似乎并不奏效。
每当我改变屏幕的宽度时,"当前日期 "条并没有和其他条保持相同的相对位置。不过,正常的日期条似乎还能正常工作。
有没有更好的更一致的方法来实现这个问题?
这里有一个jsfiddle,包含完整的html和css以及 "当前日期 "行的类。
.current-date-bar {
background-color: green;
height:110%;
width: 3px;
margin-left:35%;
position: absolute;
top: 0;
z-index:100;
}
我得到了这个工作,但我必须将当前日期条添加到每一行中,并使用一个边距偏移。我不知道是否有更好的方法来实现这个功能,但是如果有人好奇我是如何做到的,我做了一个更新的fiddle。
<div class="centered-contents" style="margin-top: 50px;">
<div class="timeline">
<div class="timeline-container">
<div class="timeline-date">
<div class="timeline-date-label">
Date 1
</div>
<div class="timeline-date-contents">
<div class="date-bar timeline-tooltip" style="width: 38%; margin-left:0%;">
<span class="timeline-tooltiptext">03/30/2020 - 04/30/2020</span>
</div>
<div class="current-date-bar"></div>
</div>
</div>
<div class="timeline-date timeline-date-alt">
<div class="timeline-date-label">
Date 2
</div>
<div class="timeline-date-contents">
<div class="date-bar timeline-tooltip" style="width: 70%; margin-left:10%;">
<span class="timeline-tooltiptext">04/12/2020 - 08/19/2020</span>
</div>
<div class="current-date-bar"></div>
</div>
</div>
<div class="timeline-date">
<div class="timeline-date-label">
Date 3
</div>
<div class="timeline-date-contents">
<div class="date-bar timeline-tooltip" style="width: 30%; margin-left:60%;">
<span class="timeline-tooltiptext">06/30/2020 - 09/14/2020</span>
</div>
<div class="current-date-bar"></div>
</div>
</div>
<div class="timeline-date timeline-date-alt last-date">
<div class="timeline-date-label">
Date 4
</div>
<div class="timeline-date-contents">
<div class="date-bar timeline-tooltip" style="width: 10px; margin-left:100%;">
<span class="timeline-tooltiptext">10/18/2020</span>
</div>
<div class="current-date-bar">
<div class="current-date-bottom"></div>
<div class="current-date-text">
<span>04/25/2020</span>
</div>
</div>
</div>
</div>
</div>
</div>
.centered-contents {
display: flex;
justify-content: center;
align-items: center;
width: 100%;
margin-top: 20px;
}
.timeline {
width: 90%;
color: #7e7e7e;
}
.timeline-container {
width: 100%;
border: solid 1px #ccc;
position: relative;
}
.timeline-date {
display: flex;
flex-direction: row;
flex-wrap: wrap;
width: 100%;
align-items: center;
}
.last-date {
border-bottom: 0;
}
.timeline-date-label,
.timeline-date-contents {
padding: 10px;
display: flex;
flex-direction: column;
flex-basis: 100%;
flex: 1;
}
.timeline-date-contents {
padding: 10px 10px 0px 0px;
}
.timeline-date-label {
min-width:100px;
max-width: 100px;
word-wrap: break-word;
border-right: solid 1px #ccc;
text-align: right;
}
.timeline-current-date {
margin-left: 100px;
}
.date-bar {
background-color: #7BAAF7;
height:20px;
}
.date-bar:hover {
background-color: #9bb5de;
}
.current-date-bar {
background-color: green;
width: 2px;
z-index: 50;
height: 40px;
margin-left: 25%;
margin-top:-30px;
}
.current-date-text {
position: absolute;
margin-left: -35px;
top: 100%;
padding-top: 7px;
}
.current-date-bottom {
position: absolute;
top: 100%;
width:2px;
background-color: green;
height: 7px;
}
.timeline-date-alt {
background-color: #e6e6e6;
}
.timeline-tooltip {
position: relative;
display: inline-block;
}
.timeline-tooltip .timeline-tooltiptext {
visibility: hidden;
width: 120px;
background-color: black;
color: #fff;
text-align: center;
border-radius: 6px;
padding: 5px 0;
position: absolute;
z-index: 200;
top: 150%;
left: 50%;
margin-left: -60px;
}
.timeline-tooltip .timeline-tooltiptext::after {
content: "";
position: absolute;
bottom: 100%;
left: 50%;
margin-left: -5px;
border-width: 5px;
border-style: solid;
border-color: transparent transparent black transparent;
}
.timeline-tooltip:hover .timeline-tooltiptext {
visibility: visible;
}