【问题标题】:CSS flexbox horizontal timelineCSS flexbox 水平时间轴
【发布时间】:2017-10-12 14:08:21
【问题描述】:

我正在尝试制作一个 flexbox 水平时间轴

我有申请 :nth-child(2n) { transform: translateY(100%); } 的想法,但我遇到了很多问题来解决这个问题:

  • 时间线之前的内容进入时间线(无法清除它们)
  • 向父级添加overflow-x: auto; 也会自动添加overflow-y,如果您隐藏overflow-y,则只有一半时间线可见

还有另一种方法可以让 flexbox 成为可能吗?我也尝试了边距,但项目位置没有很好地居中

@import "compass/css3";

.flex-container {
  padding: 0;
  margin: 0;
  list-style: none;
  
  display: flex;
  
  /* This line make an undesired Y scrollbar */
  overflow-x: auto;
}

.flex-item {
  background: tomato;
  width: 200px;
  height: 150px;

  color: white;
  
  display: flex;
  align-self: flex-end;
  align-items: flex-end;
  justify-content: center;
}

.flex-item:nth-child(2n) {
  align-self: flex-end;
  align-items: flex-start;
  transform: translateY(100%);
}


/* Random heights for testing */
.flex-item:nth-child(5) {
  height: 100px;
}

.flex-item:nth-child(3) {
  height: 200px;
}

.flex-item:nth-child(4) {
  height: 100px;
}

.flex-item:nth-child(6) {
  height: 200px;
}
<div>
  <p>This text is ok, but there is an undesired Y scrollbar on the timeline</p>
</div>

<ul class="flex-container">
  <li class="flex-item"><span>text</span></li>
  <li class="flex-item"><span>text</span></li>
  <li class="flex-item"><span>text</span></li>
  <li class="flex-item"><span>text</span></li>
  <li class="flex-item"><span>text</span></li>
  <li class="flex-item"><span>text</span></li>
</ul>

<div>
  <p>This text should go after the timeline</p>
</div>

【问题讨论】:

  • “时间线进入之前的内容”是什么意思?
  • 嗨 rebecca,检查示例代码:codepen.io/anon/pen/aLjEdo 如何使用 transform: translateY(100%); 转换时间线的一半@ 我无法检测到底部元素的高度
  • 在 SO 上,您应该尝试自己编写代码。在 doing more research 之后,如果您有问题,您可以发布您尝试过的内容,并清楚地解释什么不起作用并提供一个 Minimal, Complete, and Verifiable example.
  • 我使用您的函数添加了 sn-p 代码并对其进行了更新以使其更清晰,希望现在更清晰

标签: html css timeline


【解决方案1】:

这是一个蹩脚但可行的解决方案;-)

将它作为第一个子元素添加到 flexbox(不过你应该整理一下)。

如果您希望position:absolute/relative 覆盖在时间线的顶部而不是之前,您可以使用它。

@import "compass/css3";
.flex-container {
  padding: 0;
  margin: 0;
  list-style: none;
  display: flex;
  /* Uncomment this line to see the issue */
  /*overflow-x: auto;*/
}

.flex-item {
  background: tomato;
  width: 200px;
  height: 150px;
  color: white;
  display: flex;
  align-self: flex-end;
  align-items: flex-end;
  justify-content: center;
}

.flex-item:nth-child(2n) {
  align-self: flex-end;
  align-items: flex-start;
  transform: translateY(100%);
}


/* Random heights for testing */

.flex-item:nth-child(5) {
  height: 100px;
}

.flex-item:nth-child(3) {
  height: 200px;
}

.flex-item:nth-child(4) {
  height: 100px;
}

.flex-item:nth-child(6) {
  height: 200px;
}
<ul class="flex-container">
  <div style="clear: both;">
    <p>This text should go before</p>
  </div>
  <li class="flex-item"><span>text</span></li>
  <li class="flex-item"><span>text</span></li>
  <li class="flex-item"><span>text</span></li>
  <li class="flex-item"><span>text</span></li>
  <li class="flex-item"><span>text</span></li>
  <li class="flex-item"><span>text</span></li>
</ul>

【讨论】:

  • 嗨乔尔,谢谢,但代码示例文本是错误的,我不想要之前的文本,我想要时间线之后的文本,但是没有检测到它是如何翻译的底部高度,我也得到了一个不需要的 Y滚动条,我正在调查删除
  • 请制作一个 jpg 并将其添加到您确切想要什么的问题中,这一切都不清楚。我尽力展示我认为你想要的东西。
  • 嗨乔尔,你是对的,第一个代码示例是错误的,我更新了它,但我的主要问题来自于当我尝试设置 overflow-x: auto; 时自动添加的 Y 滚动条并且不可能让时间线旁边的内容位于其之前
【解决方案2】:

由于您使用translateY 将您的项目向下移动,因此您需要相应地定位后面的所有内容

我刚刚在你的 div 中添加了一个 other_content 类,设置 position:relativetop:200px

虽然您需要记住调整 top 值,这取决于您的项目有多大

.flex-item {
  background: tomato;
  width: 200px;
  height: 150px;

  color: white;
  
  display: flex;
  align-self: flex-end;
  align-items: flex-end;
  justify-content: center;
}

.flex-item:nth-child(2n) {
  align-self: flex-end;
  align-items: flex-start;
  transform: translateY(100%);
}


/* Random heights for testing */
.flex-item:nth-child(5) {
  height: 100px;
}

.flex-item:nth-child(3) {
  height: 200px;
}

.flex-item:nth-child(4) {
  height: 100px;
}

.flex-item:nth-child(6) {
  height: 200px;
}
.other_content{
  position:relative;
  top:200px;
}
<div>
  <p>This text is ok, but there is an undesired Y scrollbar on the timeline</p>
</div>

<ul class="flex-container">
  <li class="flex-item"><span>text</span></li>
  <li class="flex-item"><span>text</span></li>
  <li class="flex-item"><span>text</span></li>
  <li class="flex-item"><span>text</span></li>
  <li class="flex-item"><span>text</span></li>
  <li class="flex-item"><span>text</span></li>
</ul>

<div class="other_content">
  <p>This text should go after the timeline</p>
</div>

【讨论】:

  • 您需要在此处发布您的答案的代码和标记,而不是第三方网站,该网站明天可能会更改或消失,不会帮助任何人并使您的答案毫无用处。此外,不要回答诸如此类不提供所需代码的题外话问题。 meta.stackoverflow.com/questions/276572/…
  • 啊,抱歉,当我回答时,它没有显示为离题。而且我认为添加有关添加的类和属性的行就足够了。以后我会牢记这一点
  • 任何不显示代码或标记来说明 SO 上的问题的问题都是题外话,最终将被关闭。
  • 它开了 45 分钟,所以发生了错误
  • 没关系。没有代码的问题和答案是题外话,将被关闭。有时我们需要一段时间才能找到它。
猜你喜欢
  • 2018-08-20
  • 1970-01-01
  • 1970-01-01
  • 2017-03-26
  • 2023-03-25
  • 1970-01-01
  • 1970-01-01
  • 2020-09-21
  • 1970-01-01
相关资源
最近更新 更多