jQuery创建动画折线时间轴

  • 源码大小:6.73KB
  • 所需积分:1积分
  • 源码编号:19JP-3615
  • 浏览次数:666次
  • 最后更新:2023年07月02日
  • 所属栏目:图表
本站默认解压密码:19jp.com 或 19jp_com

简介

将各种事物的图形时间线放在一起,可以形成一个很棒的视觉效果。一个好的时间线可以帮助讲述时间线的故事,并让观众处于那个时刻或情况。

在本文中,我们将使用jQuery和HTML/CSS创建一个折线时间线,用户可以通过单击多边形链中的顶点在事件和事件细节之间切换。

这种类型的时间线中的视觉方面真的很突出&真的可以帮助您将数据变为现实!

请参阅实际操作:

如何使用它:

1.为折线时间线构建HTML。

  1. <div class="line-timeline">
  2. <div class="spandiv">
  3. <span id="timeline1"class="active-timeline"></span>
  4. <span id="timeline2"></span>
  5. <span id="timeline3"></span>
  6. <span id="timeline4"></span>
  7. <span id="timeline5"></span>
  8. <span id="timeline6"></span>
  9. </div>
  10. <div class="dem dem1"></div>
  11. <div class="dem dem2"></div>
  12. </div>

2.将您自己的事件添加到时间线上。

  1. <div class="content-timeline">
  2. <p class="active-content-timeline">
  3. Event 1 Active
  4. </p>
  5. <p>
  6. Event 2
  7. </p>
  8. <p>
  9. Event 3
  10. </p>
  11. <p>
  12. Event 4
  13. </p>
  14. <p>
  15. Event 5
  16. </p>
  17. <p>
  18. Event 6
  19. </p>
  20. </div>

3.时间线的主要CSS。

  1. /* timeline css */
  2. .line-timeline {
  3. position: relative;
  4. }
  5.  
  6. .line-timeline div.spandiv {
  7. width: 100%;
  8. height: 300px;
  9. position: relative;
  10. }
  11.  
  12. .dem {
  13. position: absolute;
  14. width: 96%;
  15. height: 150px;
  16. margin: auto;
  17. left: 0;
  18. right: 0;
  19. }
  20.  
  21. .dem1 {
  22. top: 0;
  23. border-top: 4px solid #ffffff;
  24. border-left: 4px solid #ffffff;
  25. border-bottom: 4px solid #ffffff;
  26. }
  27.  
  28. .dem2 {
  29. top: 150px;
  30. border-right: 4px solid #ffffff;
  31. border-bottom: 4px solid #ffffff;
  32. }
  33.  
  34. .line-timeline div.spandiv span {
  35. position: absolute;
  36. width: 30px;
  37. height: 30px;
  38. background: white;
  39. border-radius: 50px;
  40. border: 8px solid #525050;
  41. z-index: 2;
  42. cursor: pointer;
  43. transition: .3s ease-out;
  44. }
  45.  
  46. .line-timeline div.spandiv span#timeline1 {
  47. right: -10px;
  48. top: -13px;
  49. }
  50.  
  51. .line-timeline div.spandiv span#timeline2 {
  52. left: -10px;
  53. top: -13px;
  54. }
  55.  
  56. .line-timeline div.spandiv span#timeline3 {
  57. left: -10px;
  58. top: 0;
  59. bottom: 0;
  60. margin: auto;
  61. }
  62.  
  63. .line-timeline div.spandiv span#timeline4 {
  64. right: -10px;
  65. top: 0;
  66. bottom: 0;
  67. margin: auto;
  68. }
  69.  
  70. .line-timeline div.spandiv span#timeline5 {
  71. right: -10px;
  72. bottom: -13px;
  73. }
  74.  
  75. .line-timeline div.spandiv span#timeline6 {
  76. left: -14px;
  77. bottom: -13px;
  78. }
  79.  
  80. .line-timeline div.spandiv span:hover {
  81. background: black;
  82. border: 11px solid white;
  83. }
  84.  
  85. .line-timeline div.spandiv span.active-timeline {
  86. background: black;
  87. border: 11px solid white;
  88. }
  89.  
  90.  
  91. /* timeline content css */
  92. .content-timeline {
  93. color: white;
  94. text-align: justify;
  95. line-height: 2;
  96. font-size: 19px;
  97. padding: 25px;
  98. position: relative;
  99. }
  100.  
  101. .content-timeline p {
  102. opacity: 0;
  103. visibility: hidden;
  104. transition: .3s ease-in-out;
  105. position: absolute;
  106. right: 0;
  107. left: 0;
  108. top: 0;
  109. bottom: 0;
  110. margin: auto;
  111. padding: 15px;
  112. }
  113.  
  114. .content-timeline p.active-content-timeline {
  115. opacity: 1;
  116. visibility: visible;
  117. }

4.在文档中加载必要的jQuery JavaScript库。

  1. <script src="/path/to/cdn/jquery.min.js"></script>

5.用于激活折线时间线的主脚本。

  1. $(document).ready(function () {
  2.  
  3. var timelineSpan = $(".line-timeline").find("span");
  4. var contentTimeline = $(".content-timeline").find("p");
  5.  
  6. timelineSpan.click(function () {
  7. var indexSpan = $(this).index();
  8. contentTimeline.removeClass("active-content-timeline");
  9. setTimeout(function () {
  10. contentTimeline.eq(indexSpan).addClass("active-content-timeline");
  11. }, 600)
  12. timelineSpan.removeClass("active-timeline");
  13. $(this).addClass("active-timeline");
  14. console.log($(this).index());
  15. });
  16.  
  17. var timerTimeline = setInterval(autoTimeline, 3000);
  18. $('.timeline').hover(function (ev) {
  19. clearInterval(timerTimeline);
  20. }, function (ev) {
  21. timerTimeline = setInterval(autoTimeline, 3000);
  22. });
  23.  
  24. function autoTimeline() {
  25. if ($("#timeline6").hasClass("active-timeline")) {
  26. timelineSpan.removeClass("active-timeline");
  27. contentTimeline.removeClass("active-content-timeline");
  28. timelineSpan.eq(0).addClass("active-timeline");
  29. contentTimeline.eq(0).addClass("active-content-timeline");
  30.  
  31. } else {
  32. var indexSpanActive = $(".line-timeline").find(".active-timeline").index();
  33. var indexContentTimelineActive = $(".content-timeline").find(".active-content-timeline").index();
  34.  
  35. timelineSpan.removeClass("active-timeline");
  36. contentTimeline.removeClass("active-content-timeline");
  37.  
  38. timelineSpan.eq(indexSpanActive + 1).addClass("active-timeline");
  39. contentTimeline.eq(indexContentTimelineActive + 1).addClass("active-content-timeline");
  40. }
  41. }
  42.  
  43. })

预览截图