|
| 1 | +<!DOCTYPE html> |
| 2 | +<html lang="en"> |
| 3 | + <head> |
| 4 | + <meta charset="UTF-8" /> |
| 5 | + <meta http-equiv="X-UA-Compatible" content="IE=edge" /> |
| 6 | + <meta name="viewport" content="width=device-width, initial-scale=1.0" /> |
| 7 | + <title>进度统计</title> |
| 8 | + <style> |
| 9 | + .toast-wrapper { |
| 10 | + position: fixed; |
| 11 | + width: 100%; |
| 12 | + top: 20px; |
| 13 | + } |
| 14 | + .toast { |
| 15 | + padding: 20px; |
| 16 | + color: white; |
| 17 | + background: #ff0062; |
| 18 | + border-radius: 10px; |
| 19 | + box-shadow: 1px 3px 5px rgba(0, 0, 0, 0.2); |
| 20 | + max-width: 400px; |
| 21 | + margin: 0 auto; |
| 22 | + } |
| 23 | + .animation { |
| 24 | + opacity: 0; |
| 25 | + animation: fade 4s ease; |
| 26 | + } |
| 27 | + |
| 28 | + @keyframes fade { |
| 29 | + 0% { |
| 30 | + opacity: 0; |
| 31 | + } |
| 32 | + 12.5% { |
| 33 | + opacity: 0.5; |
| 34 | + } |
| 35 | + 25% { |
| 36 | + opacity: 1; |
| 37 | + } |
| 38 | + 50% { |
| 39 | + opacity: 1; |
| 40 | + } |
| 41 | + 75% { |
| 42 | + opacity: 1; |
| 43 | + } |
| 44 | + 87.5% { |
| 45 | + opacity: 0.5; |
| 46 | + } |
| 47 | + 100% { |
| 48 | + opacity: 0; |
| 49 | + } |
| 50 | + } |
| 51 | + </style> |
| 52 | + </head> |
| 53 | + <body> |
| 54 | + <div class="container"> |
| 55 | + <div class="toast-wrapper"> |
| 56 | + <div class="toast animation"></div> |
| 57 | + </div> |
| 58 | + </div> |
| 59 | + |
| 60 | + <script> |
| 61 | + const course = { |
| 62 | + all: 131 /** 课程总节数 */, |
| 63 | + hasLearned: 40 /** 初始化的已学习的节数 */, |
| 64 | + todayTarget: 5 /** 今日需要完成的课程节数目标 */, |
| 65 | + todayComplete: 0 /** 今日已学习完成课程节数 */, |
| 66 | + }; |
| 67 | + |
| 68 | + /** |
| 69 | + * 完成状态包括: |
| 70 | + * 1: 未开始('not started') |
| 71 | + * 2: 部分完成('part complete') |
| 72 | + * 3. 已完成('complete') |
| 73 | + * 4. 超额完成('overComplete') |
| 74 | + */ |
| 75 | + const learnHistory = [ |
| 76 | + { |
| 77 | + date: "2021-07-06", |
| 78 | + todayTarget: 5, |
| 79 | + todayComplete: 0, |
| 80 | + complete: "unfinished", |
| 81 | + }, |
| 82 | + ]; |
| 83 | + |
| 84 | + function getTodayDate() { |
| 85 | + const date = new Date(); |
| 86 | + const year = date.getFullYear(); |
| 87 | + const month = date.getMonth() + 1; |
| 88 | + const day = date.getDate(); |
| 89 | + let monthStr = month === 0 || month >= 10 ? month : `0${month}`; |
| 90 | + let dayStr = day === 0 || day >= 10 ? day : `0${day}`; |
| 91 | + return `${year}-${monthStr}-${dayStr}`; |
| 92 | + } |
| 93 | + |
| 94 | + function countCompleteStatus() { |
| 95 | + const historyCount = learnHistory |
| 96 | + .map((item) => item.todayComplete) |
| 97 | + .reduce( |
| 98 | + (initCount, countItem) => initCount + countItem.todayComplete |
| 99 | + ); |
| 100 | + const totalProgress = |
| 101 | + (((course.hasLearned + historyCount) / course.all) * 100).toFixed(2) + |
| 102 | + "%"; |
| 103 | + let todayProgress = 0; |
| 104 | + const todayDate = getTodayDate(); |
| 105 | + const dateFindResult = learnHistory.find( |
| 106 | + (item) => item.date === todayDate |
| 107 | + ); |
| 108 | + if (dateFindResult) { |
| 109 | + todayProgress = |
| 110 | + (dateFindResult.todayComplete / dateFindResult.todayTarget).toFixed( |
| 111 | + 2 |
| 112 | + ) + "%"; |
| 113 | + } else { |
| 114 | + todayProgress = todayProgress.toFixed(2) + "%"; |
| 115 | + } |
| 116 | + return { |
| 117 | + allTarget: course.all, |
| 118 | + totalProgress, |
| 119 | + todayTarget: dateFindResult ? dateFindResult.todayTarget : 0, |
| 120 | + todayProgress, |
| 121 | + }; |
| 122 | + } |
| 123 | + |
| 124 | + function toast() { |
| 125 | + const { allTarget, totalProgress, todayTarget, todayProgress } = |
| 126 | + countCompleteStatus(); |
| 127 | + const totalProgressDesc = `目标课程总共${allTarget}节,目前学习总进度:${totalProgress};`; |
| 128 | + const todayProgressDesc = `今日需要学习的课程共${todayTarget}节, 今天学习进度:${todayProgress};`; |
| 129 | + const elem = document.querySelector(".toast-wrapper .toast"); |
| 130 | + elem.textContent = totalProgressDesc + todayProgressDesc; |
| 131 | + } |
| 132 | + toast(); |
| 133 | + </script> |
| 134 | + </body> |
| 135 | +</html> |
0 commit comments