【问题标题】:Consistent left margin percentage spacing for a horizontal, stacked timeline水平堆叠时间线的一致左边距百分比间距
【发布时间】:2020-08-19 10:20:20
【问题描述】:

我正在制作一个时间轴,其中包含每个日期或日期范围的行,并用条形表示时间跨度。我想我会得到最早日期和最晚日期之间的天数,然后计算宽度和左边距或每个日期栏的百分比(所以总时间的 x% 将是宽度,以及开始日期和最早日期除以总时间将是左边距百分比。

我还希望在显示当前日期的所有其他行之上有一个垂直条。不过,我最终得到的结果似乎不起作用。

每当我更改屏幕宽度时,“当前日期”栏不会与其他栏保持相同的相对位置。不过,正常的日期栏似乎运行正常。

有没有更好/更一致的方式来实现这一点?

这是一个带有完整 html 和 css 的 jsfiddle 以及“当前日期”行的类:

https://jsfiddle.net/cLxw5atv/

.current-date-bar {
background-color: green; 
height:110%; 
width: 3px; 
margin-left:35%;
position: absolute;
top: 0;
z-index:100;
}

【问题讨论】:

    标签: css percentage timeline spacing


    【解决方案1】:

    我得到了这个工作,但我必须将当前日期栏添加到每一行并使用边距偏移量。我不确定是否有更好的方法来实现这一点,但如果有人好奇我是如何工作的,我做了一个更新的小提琴:

    https://jsfiddle.net/bnztvkL4/1/

    <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;
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-02-13
      • 1970-01-01
      • 1970-01-01
      • 2015-12-26
      • 2016-10-19
      相关资源
      最近更新 更多