[英]Event timeline with animation
可以為背景設置動畫。 請檢查下面的示例
.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>
您的 TimelineConnector 可以更新為包含 CSS class。
您可以在高度為 0% 的灰線上方放置一條綠線,並使用 animation 使其增長到 100%。 然后使用一點javaScript (jQuery)
,您可以啟動 animation,將 class 添加到該行。 與綠色圓圈相同(在這種情況下,延遲將 class 添加到圓圈以更改縱橫比,與您在 animation 中設置的時間相同。)
像這樣:
$('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>
我添加了一個按鈕來啟動 animation。
從 Material UI 中查看Vertical Stepper
你可以試試這個
/* 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>
你也可以檢查沙盒
在這里,我在所有li
元素中添加了active
class 但如果你想激活兩個步驟,那么只應用前兩個 li(即 class 是有條件的)
我已經修改了您的沙箱以使其正常工作: https://codesandbox.io/s/animable-timeline-reactjs-tiofz
對於 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>
將App
轉換為有狀態組件,以便我們可以維護 animation 狀態。 在構造函數中,為每個條目添加id
、 startAnim
並checked
state。 在這里,我們將設置startAnim
標志以在相應的 TimelineConnector 上啟動 animation。 和checked
用於控制檢查標記圓圈。
如果this.props.startAnim
為 true,則在TimelineConnector
中將 class 設置為 green-progress。 還添加了onAnimationEnd
處理程序作為{() => this.props.onAnimDone(this.props.id)}
。 這告訴 App 組件 animation 已在此組件上使用id
完成。
在TimelineDot
中使用props.event.checked
設置檢查狀態。
在 App 中添加了一個生命周期鈎子componentDidMount
,當所有組件都添加到實際 DOM 時,它會被調用。 在鈎子中選中第一個圓圈並在第一個 TimelineConnector 上啟動 animation。
當 TimelineConnector 完成 animation 后,它會在 App 中調用startNextAnim
。 在該方法中,您首先完成最后一個條目的復選標記。 如果條目具有status:true
,則開始下一個 animation 。
從個人經驗來看,我建議您先查看 CSS 中的通用 animation。 這是一個可以幫助您了解基礎知識和一些高級內容的頁面
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.