简体   繁体   English

animation 的事件时间线

[英]Event timeline with animation

What i am trying to build is a vertical timeline component with some animation.我正在尝试构建的是带有一些 animation 的垂直时间线组件。 The animation which am trying is that it starts from the first circle, whichever item has status true the vertical line will draw from top to bottom meanwhile whichever steps gets completed will change from round circle to completed checkmark when the line crosses it.正在尝试的 animation 是它从第一个圆圈开始,无论哪个项目的状态为 true,垂直线将从上到下绘制,同时无论哪个步骤完成,当线穿过它时,将从圆形变为完成复选标记。

How can I achieve the above animation on this, I have tried so far but didn't know how to achieve the above.我怎样才能在这个上面实现上面的animation,我到目前为止已经尝试过,但不知道如何实现上面的。

What am i trying to achieve this我想达到什么目的

sandbox沙盒

Any help is appreciated.任何帮助表示赞赏。

It's possible to animate a background.可以为背景设置动画。 Please check the example below请检查下面的示例

 .bg { min-height: 300px; width: 10px; background: linear-gradient(0, red, red) no-repeat; background-size: 100% 0; animation: gradient 15s ease infinite; } @keyframes gradient { 0% { background-size: 100% 0; } 100% { background-size: 100% 100%; } }
 <div class="bg"></div>

Your TimelineConnector could be updated with include of that CSS class.您的 TimelineConnector 可以更新为包含 CSS class。

You can put a green line above grey line with 0% height and make it grow to 100% with an animation.您可以在高度为 0% 的灰线上方放置一条绿线,并使用 animation 使其增长到 100%。 Then with a bit of javaScript (jQuery) you can start the animation adding a class to that line.然后使用一点javaScript (jQuery) ,您可以启动 animation,将 class 添加到该行。 Same with the green circle (in this case delaying adding a class to the circle to change aspect same time as you set in the animation.)与绿色圆圈相同(在这种情况下,延迟将 class 添加到圆圈以更改纵横比,与您在 animation 中设置的时间相同。)

like this:像这样:

 $('button').click(function(){ $('.line2').addClass("animation"); setTimeout( function() { $('.circle').addClass("circlegreen"); }, 1500); });
 .circle { height:30px; width:30px; border-radius:50%; border:8px solid #ddd; }.line { height:90px; width:8px; background-color:#ddd; margin-left:19px; position:relative; }.circlegreen { background-color:green; border:8px solid green; }.animation { animation-name:timeline; animation-duration:1.5s; animation-iteration-count:1; animation-fill-mode:forwards; animation-timing-function:linear; position:absolute; top:0; left:0; width:100%; height:0%; background-color:green; } @keyframes timeline { from {height:0%;} to {height:100%;} }
 <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script> <button>Click</button> <div class="circle circlegreen"></div> <div class="line"> <div class="line2"></div> </div> <div class="circle"></div>

I have added a button to start animation.我添加了一个按钮来启动 animation。

Check out Vertical Stepper from Material UI从 Material UI 中查看Vertical Stepper

You can try this你可以试试这个

 /* timeline css */ @keyframes fill-color { 0% { height: 0; } 100% { height: 100%; } } @keyframes fill-color1 { 0% { height: 0; } 100% { height: 50%; } } @keyframes scaleup { 0% { transform: scale(0); } 100% { transform: scale(1); } } @keyframes fade { 0% { color: rgba(black, 0.4); } 100% { color: rgba(black, 1); } } body { margin: 0; padding: 0; }.timeline { padding: 0; list-style: none; margin: 32px; overflow: hidden; position: relative; }.details { margin-left: 48px; border-bottom: 1px solid #f2f2f2; min-height: 85px; display: flex; justify-content: center; flex-direction: column; }.list, .list-content { position: relative; width: 100%; }.list-content::before, .list-content::after { content: ""; display: block; position: absolute; left: 0; transition: 0.2s all linear; width: 0.714rem; height: 0.714rem; border-radius: 50%; background-color: gray; top: 50%; z-index: 3; margin-left: 0.35rem; margin-top: rem(-8px); }.list-content::after { z-index: 2; }.list { position: relative; width: 100%; }.list.active.list-content:before { transform: scale(0); width: 17px; height: 17px; border: 2px solid white; background-color: red; background-image: url("https://upload.wikimedia.org/wikipedia/commons/thumb/2/27/White_check.svg/2048px-White_check.svg.png"); background-position: center; background-repeat: no-repeat; background-size: 9px 7px; margin-left: 0; margin-top: -8px; animation: scaleup 0.4s forwards; }.list:before, .list:after { content: ""; display: block; position: absolute; left: 0; transition: 0.2s all linear; width: 0.214rem; margin-left: 0.6rem; }.list:before { background: #f2f2f2; height: 100%; }.list:after { background: red; height: 0; z-index: 1; }.list:before { top: -50%; }.list.active:after { top: 0; animation: fill-color 0.4s forwards; }.list:last-child:after { display: none; }.list:last-child.active:after { display: block; bottom: 50%; animation: fill-color1 0.4s forwards; }.list:last-child.details { border-bottom: none; }.list:first-child:before { display: none; }.list:first-child.active:after { animation: fill-color1 0.4s forwards; top: 50%; }.list:first-child.active:after { animation-delay: 1s; }.list:first-child.active.list-content:before { animation-delay: 0.5s; }.list:nth-child(2).active:after { animation-delay: 2s; }.list:nth-child(2).active.list-content:before { animation-delay: 2s; }.list:nth-child(3).active:after { animation-delay: 3s; }.list:nth-child(3).active.list-content:before { animation-delay: 3s; }.list:nth-child(4).active:after { animation-delay: 4s; }.list:nth-child(4).active.list-content:before { animation-delay: 4.15s; }
 <body> <ul class="timeline"> <li class="list active"> <div class="list-content"> <div class="details"> <h4 class="status-title">Step 1</h4> </div> </div> </li> <li class="list active"> <div class="list-content"> <div class="details"> <h4 class="status-title">Step 2</h4> </div> </div> </li> <li class="list active"> <div class="list-content"> <div class="details"> <h4 class="status-title">Step 3</h4> </div> </div> </li> <li class="list active"> <div class="list-content"> <div class="details"> <h4 class="status-title">Step 4</h4> </div> </div> </li> </ul> </body>

you can also check onSandbox你也可以检查沙盒

here I have added active class in all li elements but if you want to two steps active so apply only first two li (ie that class is conditional)在这里,我在所有li元素中添加了active class 但如果你想激活两个步骤,那么只应用前两个 li(即 class 是有条件的)

I've modified your sandbox to make it work: https://codesandbox.io/s/animable-timeline-reactjs-tiofz我已经修改了您的沙箱以使其正常工作: https://codesandbox.io/s/animable-timeline-reactjs-tiofz

  1. For animation I used following CSS:对于 animation,我使用了以下 CSS:

     div { height: 200px; width: 10px; }.green-progress { background: linear-gradient(0, #00a36c, #00a36c) no-repeat, #ccc; background-size: 100% 0; animation: progressAnim 3s linear infinite forwards; } @keyframes progressAnim { 0% { background-size: 100% 0; } 100% { background-size: 100% 100%; } }
     <div class="green-progress"></div>

    To animate actual time line we'll remove vertical bar from first entry and there will be only checked circle.为了动画实际时间线,我们将从第一个条目中删除垂直条,并且只有选中的圆圈。 From second entry onwards we'll have a vertical bar and checked circle.从第二个条目开始,我们将有一个垂直条和选中的圆圈。 To make them consistent they've been shifted upwards.为了使它们保持一致,它们已向上移动。 To show progress, the bar will fill and then circle will be checked.为了显示进度,该条将填充,然后将检查圆圈。

  2. Converted App to stateful component so that we can maintain animation states.App转换为有状态组件,以便我们可以维护 animation 状态。 In constructor, for each entry added id , startAnim , and checked state.在构造函数中,为每个条目添加idstartAnimchecked state。 Here, we'll set startAnim flag to start animation on corresponding TimelineConnector.在这里,我们将设置startAnim标志以在相应的 TimelineConnector 上启动 animation。 and checked is used to control checkmarking the circle.checked用于控制检查标记圆圈。

  3. In TimelineConnector set class to green-progress if this.props.startAnim is true.如果this.props.startAnim为 true,则在TimelineConnector中将 class 设置为 green-progress。 Also added onAnimationEnd handler as {() => this.props.onAnimDone(this.props.id)} .还添加了onAnimationEnd处理程序作为{() => this.props.onAnimDone(this.props.id)} This tells App component that animation is done on this component with id .这告诉 App 组件 animation 已在此组件上使用id完成。

  4. In TimelineDot used props.event.checked to set the checked status.TimelineDot中使用props.event.checked设置检查状态。

  5. In App added a lifecycle hook componentDidMount which will get called when all components gets added to actual DOM.在 App 中添加了一个生命周期钩子componentDidMount ,当所有组件都添加到实际 DOM 时,它会被调用。 In the hook you checkmark the first circle and start animation on first TimelineConnector.在钩子中选中第一个圆圈并在第一个 TimelineConnector 上启动 animation。

  6. When TimelineConnector is done with the animation, it calls startNextAnim in the App.当 TimelineConnector 完成 animation 后,它会在 App 中调用startNextAnim In the method you first complete the checkmark on last entry.在该方法中,您首先完成最后一个条目的复选标记。 And start next animation if the entry has status:true .如果条目具有status:true ,则开始下一个 animation 。


We could've added delays to each animation and ran them at once. 我们可以为每个 animation 添加延迟并立即运行它们。 But parent controlling each component and each component notifying when animation is done makes it more flexible to update the code. 但是当 animation 完成时,父控制每个组件和每个组件通知使得更新代码更加灵活。 You can have different animations for each entry, based on their state. 您可以根据 state 为每个条目设置不同的动画。

We can use react-spring animation library but things will get complicated. 我们可以使用 react-spring animation 库,但事情会变得复杂。 CSS animation is the simplest solution. CSS animation 是最简单的解决方案。

From personal experience, I'd recommend that you look at general animation in CSS first.从个人经验来看,我建议您先查看 CSS 中的通用 animation。 Here is a page that can help you with the basics and some advanced stuff这是一个可以帮助您了解基础知识和一些高级内容的页面

this page这一页

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM