time.vue 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334
  1. <template>
  2. <div class="container">
  3. <div class="setters" :style="{width:cwidth}">
  4. <div class="minutes-set">
  5. <button
  6. data-setter="minutes-plus"
  7. @click="changeTime('minutes-plus')"
  8. :disabled="isStarted"
  9. :style="{ opacity: isStarted ? 0.5 : 1 }"
  10. >
  11. +
  12. </button>
  13. <button
  14. data-setter="minutes-minus"
  15. @click="changeTime('minutes-minus')"
  16. :disabled="isStarted"
  17. :style="{ opacity: isStarted ? 0.5 : 1 }"
  18. >
  19. -
  20. </button>
  21. </div>
  22. <div class="seconds-set">
  23. <button
  24. data-setter="seconds-plus"
  25. @click="changeTime('seconds-plus')"
  26. :disabled="isStarted"
  27. :style="{ opacity: isStarted ? 0.5 : 1 }"
  28. >
  29. +
  30. </button>
  31. <button
  32. data-setter="seconds-minus"
  33. @click="changeTime('seconds-minus')"
  34. :disabled="isStarted"
  35. :style="{ opacity: isStarted ? 0.5 : 1 }"
  36. >
  37. -
  38. </button>
  39. </div>
  40. </div>
  41. <div class="circle">
  42. <svg width="300" viewBox="0 0 220 220" xmlns="http://www.w3.org/2000/svg">
  43. <g transform="translate(110,110)">
  44. <circle r="100" class="e-c-base" />
  45. <g transform="rotate(-90)">
  46. <circle
  47. r="100"
  48. class="e-c-progress"
  49. :style="{ strokeDasharray: offset }"
  50. />
  51. <g id="e-pointer" :style="{ transform: pointerTransform }">
  52. <circle cx="100" cy="0" r="8" class="e-c-pointer" />
  53. </g>
  54. </g>
  55. </g>
  56. </svg>
  57. </div>
  58. <div class="controlls">
  59. <div class="display-remain-time">{{ displayString }}</div>
  60. <!-- <button
  61. :class="{ play: !play, pause: play }"
  62. id="pause"
  63. @click="pauseTimer"
  64. ></button> -->
  65. <el-button type="primary" @click="reset" class="reset_btn">重置</el-button>
  66. </div>
  67. </div>
  68. </template>
  69. <script>
  70. export default {
  71. props: ["preTime"],
  72. data() {
  73. return {
  74. length: Math.PI * 2 * 100,
  75. offset: 0,
  76. pointerTransform: "",
  77. play: false,
  78. intervalTimer: null,
  79. wholeTime: 0,
  80. timeLeft: "",
  81. isPaused: false,
  82. isStarted: false,
  83. displayString: "",
  84. isWhole:false,
  85. cwidth: '162px'
  86. };
  87. },
  88. methods: {
  89. changeWholeTime(seconds) {
  90. if (this.wholeTime + seconds >= 0) {
  91. this.wholeTime += seconds;
  92. this.update(this.wholeTime, this.wholeTime);
  93. }
  94. },
  95. changeTime(param) {
  96. switch (param) {
  97. case "minutes-plus":
  98. this.changeWholeTime(1 * 60);
  99. break;
  100. case "minutes-minus":
  101. this.changeWholeTime(-1 * 60);
  102. break;
  103. case "seconds-plus":
  104. this.changeWholeTime(1);
  105. break;
  106. case "seconds-minus":
  107. this.changeWholeTime(-1);
  108. break;
  109. }
  110. this.displayTimeLeft(this.wholeTime);
  111. },
  112. update(value, timePercent, type) {
  113. this.offset = -this.length - (this.length * value) / timePercent;
  114. if (value === 0 && type === 1) {
  115. this.pointerTransform = `rotate(${360}deg)`;
  116. } else {
  117. this.pointerTransform = `rotate(${(360 * value) / timePercent}deg)`;
  118. }
  119. },
  120. displayTimeLeft(timeLeft, type) {
  121. //displays time on the input
  122. let minutes = Math.floor(timeLeft / 60);
  123. let seconds = timeLeft % 60;
  124. this.displayString = `${minutes < 10 ? "0" : ""}${minutes}:${
  125. seconds < 10 ? "0" : ""
  126. }${seconds}`;
  127. // displayOutput.textContent = displayString;
  128. this.update(timeLeft, this.wholeTime, type);
  129. this.updateTimeNum();
  130. },
  131. timer(seconds) {
  132. //counts time, takes seconds
  133. let remainTime = Date.now() + seconds * 1000;
  134. this.displayTimeLeft(seconds);
  135. let _this = this;
  136. _this.intervalTimer = setInterval(function () {
  137. _this.timeLeft = Math.round((remainTime - Date.now()) / 1000);
  138. if (_this.timeLeft < 0) {
  139. clearInterval(_this.intervalTimer);
  140. _this.isStarted = false;
  141. _this.play = false;
  142. // setterBtns.forEach(function (btn) {
  143. // btn.disabled = false;
  144. // btn.style.opacity = 1;
  145. // });
  146. _this.displayTimeLeft(_this.wholeTime);
  147. _this.$message.success("时间到!!!");
  148. // pauseBtn.classList.remove("pause");
  149. // pauseBtn.classList.add("play");
  150. return;
  151. }
  152. _this.displayTimeLeft(_this.timeLeft);
  153. }, 1000);
  154. },
  155. pauseTimer() {
  156. if (!this.play && !this.isPaused) {
  157. this.play = true;
  158. this.isStarted = true;
  159. this.timer(this.wholeTime);
  160. } else if (this.isPaused) {
  161. this.play = true;
  162. this.timer(this.timeLeft);
  163. this.isStarted = true;
  164. this.isPaused = this.isPaused ? false : true;
  165. } else {
  166. this.play = false;
  167. this.isStarted = true;
  168. clearInterval(this.intervalTimer);
  169. this.isPaused = this.isPaused ? false : true;
  170. }
  171. console.log(1);
  172. },
  173. reset() {
  174. clearInterval(this.intervalTimer);
  175. this.play = false;
  176. this.isStarted = false;
  177. this.isPaused = false;
  178. this.wholeTime = 0;
  179. this.timeLeft = null;
  180. this.update(this.wholeTime, this.wholeTime, 1);
  181. this.displayTimeLeft(this.wholeTime, 1);
  182. },
  183. updateTimeNum() {
  184. this.$emit("updateTimeNum", this.wholeTime);
  185. },
  186. getValue() {
  187. this.wholeTime = this.preTime;
  188. this.update(this.wholeTime, this.wholeTime, 1);
  189. this.displayTimeLeft(this.wholeTime,1);
  190. // this.$forceUpdate();
  191. },
  192. },
  193. created() {
  194. this.offset = this.length;
  195. // this.update(this.wholeTime, this.wholeTime);
  196. // this.displayTimeLeft(this.wholeTime);
  197. this.getValue();
  198. this.$nextTick(()=>{
  199. setTimeout(() => {
  200. this.cwidth = document.querySelector(".controlls") ? (document.querySelector(".controlls").offsetWidth + 'px') : 'fit-content'
  201. }, 600);
  202. })
  203. },
  204. beforeDestroy() {
  205. clearInterval(this.intervalTimer);
  206. },
  207. };
  208. </script>
  209. <style scoped>
  210. button[data-setter] {
  211. outline: none;
  212. background: transparent;
  213. border: none;
  214. font-family: "Roboto";
  215. font-weight: 300;
  216. font-size: 36px;
  217. width: 32px;
  218. /* height: 30px; */
  219. color: #409eff;
  220. cursor: pointer;
  221. }
  222. button[data-setter]:hover {
  223. opacity: 0.5;
  224. }
  225. .container {
  226. position: relative;
  227. /* top: 30px; */
  228. width: 300px;
  229. margin: 0 auto;
  230. display: flex;
  231. flex-direction: column;
  232. justify-content: center;
  233. }
  234. .setters {
  235. position: absolute;
  236. left: 50%;
  237. top: 75px;
  238. transform: translateX(-50%);
  239. display: flex;
  240. justify-content: space-between;
  241. }
  242. .minutes-set {
  243. /* float: left; */
  244. /* margin-right: 28px; */
  245. /* margin-right: 20px; */
  246. }
  247. .seconds-set {
  248. /* float: right; */
  249. }
  250. .controlls {
  251. position: absolute;
  252. top: 105px;
  253. text-align: center;
  254. min-width: fit-content;
  255. width: fit-content;
  256. left: 50%;
  257. transform: translateX(-50%);
  258. }
  259. .display-remain-time {
  260. font-family: "Roboto";
  261. font-weight: 100;
  262. font-size: 66px;
  263. color: #409eff;
  264. display: flex;
  265. flex-wrap: nowrap;
  266. min-width: fit-content;
  267. justify-content: center;
  268. white-space: nowrap;
  269. }
  270. #pause {
  271. outline: none;
  272. background: transparent;
  273. border: none;
  274. margin-top: 10px;
  275. width: 50px;
  276. height: 50px;
  277. position: relative;
  278. }
  279. .play::before {
  280. display: block;
  281. content: "";
  282. position: absolute;
  283. top: 8px;
  284. left: 16px;
  285. border-top: 15px solid transparent;
  286. border-bottom: 15px solid transparent;
  287. border-left: 22px solid #77a4d3;
  288. }
  289. .pause::after {
  290. content: "";
  291. position: absolute;
  292. top: 8px;
  293. left: 12px;
  294. width: 15px;
  295. height: 30px;
  296. background-color: transparent;
  297. border-radius: 1px;
  298. border: 5px solid #77a4d3;
  299. border-top: none;
  300. border-bottom: none;
  301. }
  302. #pause:hover {
  303. opacity: 0.8;
  304. }
  305. .e-c-base {
  306. fill: none;
  307. stroke: #b6b6b6;
  308. stroke-width: 4px;
  309. }
  310. .e-c-progress {
  311. fill: none;
  312. stroke: #6eaded;
  313. stroke-width: 4px;
  314. transition: stroke-dashoffset 0.7s;
  315. }
  316. .e-c-pointer {
  317. fill: #fff;
  318. stroke: #6eaded;
  319. stroke-width: 2px;
  320. }
  321. #e-pointer {
  322. transition: transform 0.7s;
  323. }
  324. .reset_btn {
  325. width: 100px;
  326. margin: 15px auto 0px;
  327. background: #527aa3;
  328. border: none;
  329. }
  330. </style>