time.vue 8.2 KB

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