纯 CSS 实现水平时间轴

7

我正在尝试用纯CSS制作时间轴,但是似乎遇到了一些问题。

当我尝试将时间轴div设置为overflow-x: scroll时,它仍会在y轴上滚动。

这是我的代码:

#timeline {
  height: 500px;
  width: auto;
  margin: 0;
  background: red;
}

.event {
  height: 500px;
}

.founded {
  width: 400px;
  float: left;
  background: blue;
}

.grant {
  width: 800px;
  background: yellow;
}
<ol id="timeline">
  <li class="event founded"></li>
  <li class="event grant"></li>
</ol>

我只是希望每个新增的条目都跟在前面的条目后面,而且整个内容可以水平滚动。如果有人能指点一下我,那就太棒了。
谢谢。

展示一下演示,很难想象最终结果是什么样子。你需要设置 overflow-y: hidden 来避免滚动,但你可能需要修复导致 overflow-y 开始的任何问题。 - helion3
想必您会对这个感兴趣。http://css-tricks.com/snippets/jquery/horz-scroll-with-mouse-wheel/ - Cam
2个回答

21

我刚刚刚需要制作一个这样的东西。这就是我想出来的:

body {
  padding: 25px;
  font-family: sans-serif;
}

.timeline {
  white-space: nowrap;
  overflow-x: scroll;
  padding: 30px 0 10px 0;
  position: relative;
}

.entry {
  display: inline-block;
  vertical-align: top;
  background: #13519C;
  color: #fff;
  padding: 10px;
  font-size: 12px;
  text-align: center;
  position: relative;
  border-top: 4px solid #06182E;
  border-radius: 3px;
  min-width: 200px;
  max-width: 500px;
}

.entry img {
  display: block;
  max-width: 100%;
  height: auto;
  margin-bottom: 10px;
}

.entry:after {
  content: '';
  display: block;
  background: #eee;
  width: 7px;
  height: 7px;
  border-radius: 6px;
  border: 3px solid #06182E;
  position: absolute;
  left: 50%;
  top: -30px;
  margin-left: -6px;
}

.entry:before {
  content: '';
  display: block;
  background: #06182E;
  width: 5px;
  height: 20px;
  position: absolute;
  left: 50%;
  top: -20px;
  margin-left: -2px;
}

.entry h1 {
  color: #fff;
  font-size: 18px;
  font-family: Georgia, serif;
  font-weight: bold;
  margin-bottom: 10px;
}

.entry h2 {
  letter-spacing: .2em;
  margin-bottom: 10px;
  font-size: 14px;
}

.bar {
  height: 4px;
  background: #eee;
  width: 100%;
  position: relative;
  top: 13px;
  left: 0;
}
<div class="bar"></div>
<div class="timeline">
  <div class="entry">
    <h1>1990</h1>
    <h2>Entry Title</h2>
    <img src="http://dummyimage.com/300x200/000/fff" /> Here's the info about this date
  </div>
  <div class="entry">
    <h1>1995</h1>
    Here's the info about this date
  </div>
  <div class="entry">
    <h1>2000</h1>
    Here's the info about this date
  </div>
  <div class="entry">
    <h1>2005</h1>
    Here's the info about this date
  </div>
  <div class="entry">
    <h1>2010</h1>
    Here's the info about this date
  </div>
</div>


我刚刚在你发送消息之前才加入了最后一行代码。不过还是谢谢你的回复。顺便说一下,我也很喜欢你的设计。 - faooful
这正是我想要做的,包括时间轴和空心圆作为锚点。我的唯一调整将是使用有序列表而不是divs,但它否则完美无缺。 - Aquarelle
我该如何将第一个框显示在底部,第二个框显示在顶部,以此类推?类似于这样的效果 https://prnt.sc/qn3juq - Naren Verma
@NarenVerma - 可能是类似于.entry:nth-child(2n) { // change position to top }这样的东西。如果需要,您可以利用负左右边距将它们挤在一起。 - kthornbloom

6

您也可以参考下面的链接

@import url(https://fonts.googleapis.com/css?family=Dosis:500);

body{
  background: #F1F1F1;
}

.container{
  width: 1200px;
  margin: auto;
}

.timeline{
  counter-reset: year 2016;
  position: relative;
}

.timeline li{
  list-style: none;
  float: left;
  width: 33.3333%;
  position: relative;
  text-align: center;
  text-transform: uppercase;
  font-family: 'Dosis', sans-serif;
}

ul:nth-child(1){
  color: #4caf50;
}

.timeline li:before{
  counter-increment: year;
  content: counter(year);
  width: 50px;
  height: 50px;
  border: 3px solid #4caf50;
  border-radius: 50%;
  display: block;
  text-align: center;
  line-height: 50px;
  margin: 0 auto 10px auto;
  background: #F1F1F1;
  color: #000;
  transition: all ease-in-out .3s;
  cursor: pointer;
}

.timeline li:after{
  content: "";
  position: absolute;
  width: 100%;
  height: 1px;
  background-color: grey;
  top: 25px;
  left: -50%;
  z-index: -999;
  transition: all ease-in-out .3s;
}

.timeline li:first-child:after{
  content: none;
}
.timeline li.active{
  color: #555555;
}
.timeline li.active:before{
  background: #4caf50;
  color: #F1F1F1;
}

.timeline li.active + li:after{
  background: #4caf50;
}
<h1><a href="http://developerstips.com/">DevelopersTips</a></h1>

<div class="container">
  <ul class="timeline">
    <li class="active">Bacon</li>
    <li>Rib</li>
    <li>Sausage</li>

  </ul>
</div>


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接