不需要 JavaScript,仅用 CSS 实现时间轴动画效果

前言

当我们在网站上寻找信息或者浏览内容时,一个美观且简洁的时间轴能够让我们更加清晰地了解信息的时间顺序。但是,使用 javascript 编写时间轴可能会让一些人望而却步。那么,有没有一种更简单的方法来实现时间轴呢?答案是肯定的!本文将介绍一种仅使用 css 实现的时间轴,不仅美观、简单,而且还有动画效果,让你的网站内容更加生动有趣。


实现思路

  1. 每次添加事件要调整标签中 top 的距离,每次递增;
  2. 每次添加事件要调整动画的时长,呈现均衡的效果;
  3. 每次添加事件要增加竖线的高度,根据自己需求而定。

源码如下

<template>
  <div>
    <!-- 最外层的盒子 -->
    <div class="outerBox">
      <!-- 第一大块 -->
      <!-- 左边整体的内容 -->
      <div class="dateBox">
        <h2>2021/12/31</h2>
        <div>
          <p>发生的事件</p>
          <ul>事件的详细内容事件的详细内容事件的详细内容事件的详细内容事件的详细内容</ul>
        </div>
      </div>
      <!-- 右边整体的内容 -->
      <div class="dateLeftBox" style="top: 120px;">
        <h2>2021/12/25</h2>
        <div>
          <p>发生的事件</p>
          <ul>事件的详细内容</ul>
        </div>
      </div>

      <!-- 第二大块 -->
      <!-- 左边整体的内容 -->
      <div class="dateBox" style="top: 240px;">
        <h2>2021/12/23</h2>
        <div>
          <p>发生的事件</p>
          <ul>事件的详细内容</ul>
        </div>

      </div>
      <!-- 右边整体的内容 -->
      <div class="dateLeftBox" style="top: 360px;">
        <h2>2021/12/13</h2>
        <div>
          <p>发生的事件</p>
          <ul>事件的详细内容</ul>
        </div>
      </div>

      <!-- 第三大块 -->
      <!-- 左边整体的内容 -->
      <div class="dateBox" style="top: 480px;">
        <h2>2020/12/23</h2>
        <div>
          <p>发生的事件</p>
          <ul>事件的详细内容</ul>
        </div>

      </div>
      <!-- 右边整体的内容 -->
      <div class="dateLeftBox" style="top: 600px;">
        <h2>2020/05/14</h2>
        <div>
          <p>发生的事件</p>
          <ul>事件的详细内容</ul>
        </div>
      </div>

      <!-- 第四大块 -->
      <!-- 左边整体的内容 -->
      <div class="dateBox" style="top: 720px;">
        <h2>2020/12/23</h2>
        <div>
          <p>发生的事件</p>
          <ul>事件的详细内容</ul>
        </div>

      </div>
      <!-- 右边整体的内容 -->
      <div class="dateLeftBox" style="top: 840px;">
        <h2>2020/05/14</h2>
        <div>
          <p>发生的事件</p>
          <ul>事件的详细内容</ul>
        </div>
      </div>
    </div>
  </div>
</template>

<script>
export default {};
</script>

<style scoped>
.outerBox {
  /* 竖线样式 高度根据事件的多少调整*/
  width: 5px;
  height: 900px;
  background: rgb(221, 221, 221);
  margin: 40px auto;
  position: relative;
  -webkit-animation: heightSlide 2s linear;
}

@-webkit-keyframes heightSlide {
  /* 竖线的动画效果:以百分比来规定改变发生的时间,0% 是动画的开始时间,100% 动画的结束时间 */
  0% {
    height: 0;
  }

  100% {
    height: 900px;
  }
}

.outerBox:after {
  /* 竖线末尾文字样式 */
  content: "未完待续";
  width: 100px;
  color: rgb(84, 84, 85);
  position: absolute;
  margin-left: -48px;
  text-align: center;
  bottom: -30px;
  -webkit-animation: showIn 5.5s ease;
}

.outerBox .dateBox,
.outerBox .dateLeftBox {
  /* 球球的样式 */
  position: absolute;
  margin-left: -4px;
  margin-top: -10px;
  width: 13px;
  height: 13px;
  border-radius: 50%;
  border: 4px solid rgb(221, 221, 221);
  background: rgb(31, 122, 252);
  -webkit-transition: all 0.5s;
  -webkit-animation: showIn ease;
}

.outerBox .dateBox:nth-child(1) {
  /* 第一个事件 设置动画在几秒内完成 */
  -webkit-animation-duration: 1s;
}

.outerBox .dateLeftBox:nth-child(2) {
  /* 第二个事件 设置动画在几秒内完成 */
  -webkit-animation-duration: 1.5s;
}

.outerBox .dateBox:nth-child(3) {
  /* 第三个事件 设置动画在几秒内完成 */
  -webkit-animation-duration: 2s;
}

.outerBox .dateLeftBox:nth-child(4) {
  /* 第四个事件 设置动画在几秒内完成 */
  -webkit-animation-duration: 2.5s;
}

.outerBox .dateBox:nth-child(5) {
  /* 第五个事件 设置动画在几秒内完成 */
  -webkit-animation-duration: 3s;
}

.outerBox .dateLeftBox:nth-child(6) {
  /* 第六个事件 设置动画在几秒内完成 */
  -webkit-animation-duration: 3.5s;
}

.outerBox .dateBox:nth-child(7) {
  /* 第七个事件 设置动画在几秒内完成 */
  -webkit-animation-duration: 4s;
}

.outerBox .dateLeftBox:nth-child(8) {
  /* 第八个事件 设置动画在几秒内完成 */
  -webkit-animation-duration: 4.5s;
}

@-webkit-keyframes showIn {
  /* 球球、竖线、左右的模块的动画 */
  0%,
  70% {
    opacity: 0;
  }

  100% {
    opacity: 1;
  }
}

.outerBox .dateBox h2,
.outerBox .dateLeftBox h2 {
  /* 日期的样式 */
  position: absolute;
  margin-left: -120px;
  margin-top: 3px;
  color: rgb(84, 84, 85);
  font-size: 14px;
  cursor: pointer;
  /* -webkit-animation: showIn 3s ease; */
}

.outerBox .dateLeftBox h2 {
  /* 右边日期的样式 */
  margin-left: 60px;
  width: 100px;
}

.outerBox .dateBox:hover,
.outerBox .dateLeftBox:hover {
  /* 触摸事件后球球的样式 */
  border: 4px solid rgb(195, 195, 195);
  background: rgb(143, 189, 253);
  box-shadow: 0 0 2px 2px rgba(255, 255, 255, 0.4);
}

.outerBox .dateBox div,
.outerBox .dateLeftBox div {
  /* 左右事件的样式 */
  position: absolute;
  top: 50%;
  margin-top: -10px;
  left: 50px;
  width: 300px;
  height: 22px;
  border: 2px solid rgb(84, 84, 85);
  border-radius: 6px;
  z-index: 2;
  overflow: hidden;
  cursor: pointer;
  /* -webkit-animation: showIn 5s ease; */
  -webkit-transition: all 0.5s;
}

.outerBox .dateLeftBox div {
  /* 左边事件的样式 */
  left: -337px;
}

.outerBox .dateBox div:hover,
.outerBox .dateLeftBox div:hover {
  /* 触摸事件后的高度 */
  height: 68px;
}

.outerBox .dateBox div p,
.outerBox .dateLeftBox div p {
  /* 左右事件的字体样式 */
  color: rgb(84, 84, 85);
  font-weight: bold;
  text-align: center;
}

.outerBox .dateBox:before,
.outerBox .dateLeftBox:before {
  /* 右边事件的角标样式 */
  content: "";
  position: absolute;
  top: -4px;
  left: 37px;
  width: 0px;
  height: 0px;
  border: 7px solid transparent;
  border-right: 7px solid rgb(84, 84, 85);
  z-index: -1;
  -webkit-animation: showIn 5s ease;
}

.outerBox .dateLeftBox:before {
  /* 左边事件的角标样式 */
  left: -38px;
  border: 7px solid transparent;
  border-left: 7px solid rgb(84, 84, 85);
}

.outerBox .dateBox div ul,
.outerBox .dateLeftBox div ul {
  /* 左右事件触摸展开后内容的样式 */
  list-style: none;
  width: 300px;
  padding: 4px;
  border-top: 2px solid rgb(84, 84, 85);
  color: rgb(84, 84, 85);
  font-size: 14px;
}
</style>

实现效果

在这里插入图片描述

  • 9
    点赞
  • 43
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
可以使用HTML5和CSS实现横向时间轴特效,具体实现步骤如下: 1. 首先,使用HTML创建一个时间轴容器,其中包含一个无序列表(ul)和多个列表项(li),每个列表项表示一个时间节点。 2. 使用CSS设置时间轴容器的宽度和高度,并将列表项的display属性设置为inline-block,以便它们在同一水平线上显示。 3. 通过CSS设置每个列表项的宽度、高度、背景色、边框、圆角等样式属性,以便它们具有时间轴的外观。 4. 使用CSS设置每个列表项的位置,使它们在时间轴上按照时间顺序排列。可以使用position属性将它们定位在时间轴上。 5. 为了实现鼠标悬停效果,可以使用CSS设置每个列表项的:hover样式属性。例如,可以改变背景颜色或添加阴影效果。 6. 最后,通过CSS添加动画效果实现时间轴的滑动效果。可以使用transition或animation属性来实现。 下面是一个示例代码,实现一个简单的横向时间轴特效: ``` <!DOCTYPE html> <html> <head> <style> .timeline { width: 800px; height: 100px; margin: 0 auto; position: relative; background-color: #f2f2f2; border-radius: 10px; overflow: hidden; } .timeline li { display: inline-block; width: 100px; height: 100px; background-color: #fff; border: 1px solid #ccc; border-radius: 50%; position: absolute; bottom: 0; transform: translateY(50%); transition: all 0.3s ease-in-out; } .timeline li:hover { background-color: #f2f2f2; box-shadow: 0px 0px 10px 0px rgba(0,0,0,0.5); } .timeline li:nth-child(odd) { left: 50%; transform: translateX(-50%) translateY(50%); } .timeline li:nth-child(even) { right: 50%; transform: translateX(50%) translateY(50%); } .timeline:hover li { transform: translateY(-50%); } </style> </head> <body> <div class="timeline"> <ul> <li>1</li> <li>2</li> <li>3</li> <li>4</li> <li>5</li> </ul> </div> </body> </html> ``` 在这个示例中,我们创建了一个名为“timeline”的容器,其中包含了一个无序列表。我们使用CSS设置了容器的宽度、高度、背景色和边框等样式属性。列表项具有圆形形状,并使用CSS设置了它们的位置和样式属性。 我们还添加了一个:hover样式属性,以便在鼠标悬停时改变列表项的背景颜色和添加阴影效果。最后,我们使用CSS添加了一个动画效果,使时间轴在鼠标悬停时滑动。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

水星记_

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值