uni-count-down.vue 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254
  1. <template>
  2. <view class="uni-countdown">
  3. <view
  4. v-if="showDay && d > 0"
  5. :style="{ borderColor: borderColor, color: color, background: backgroundColor }"
  6. class="uni-countdown__number "
  7. :class="[backgroundColorClass, colorClass, borderColorClass]"
  8. >
  9. {{ d }}
  10. </view>
  11. <view v-if="showDay && d > 0" :style="{ color: splitorColor }" class="uni-countdown__splitor day" :class="splitorColorClass">{{ showColon ? '天' : '天' }}</view>
  12. <view
  13. :style="{ borderColor: borderColor, color: color, background: backgroundColor }"
  14. class="uni-countdown__number "
  15. :class="[backgroundColorClass, colorClass, borderColorClass]"
  16. >
  17. {{ h }}
  18. </view>
  19. <view :style="{ color: splitorColor }" class="uni-countdown__splitor" :class="splitorColorClass">{{ showColon ? ':' : '时' }}</view>
  20. <view
  21. :style="{ borderColor: borderColor, color: color, background: backgroundColor }"
  22. class="uni-countdown__number "
  23. :class="[backgroundColorClass, colorClass, borderColorClass]"
  24. >
  25. {{ i }}
  26. </view>
  27. <view :style="{ color: splitorColor }" class="uni-countdown__splitor" :class="splitorColorClass">{{ showColon ? ':' : '分' }}</view>
  28. <view
  29. :style="{ borderColor: borderColor, color: color, background: backgroundColor }"
  30. class="uni-countdown__number "
  31. :class="[backgroundColorClass, colorClass, borderColorClass]"
  32. >
  33. {{ s }}
  34. </view>
  35. <view v-if="!showColon" :style="{ color: splitorColor }" class="uni-countdown__splitor" :class="splitorColorClass">秒</view>
  36. </view>
  37. </template>
  38. <script>
  39. export default {
  40. name: 'UniCountDown',
  41. props: {
  42. showDay: {
  43. type: Boolean,
  44. default: true
  45. },
  46. showColon: {
  47. type: Boolean,
  48. default: true
  49. },
  50. backgroundColor: {
  51. type: String,
  52. default: '#FFFFFF'
  53. },
  54. backgroundColorClass: {
  55. type: String,
  56. default: ''
  57. },
  58. borderColor: {
  59. type: String,
  60. default: '#000000'
  61. },
  62. borderColorClass: {
  63. type: String,
  64. default: ''
  65. },
  66. color: {
  67. type: String,
  68. default: '#000000'
  69. },
  70. colorClass: {
  71. type: String,
  72. default: ''
  73. },
  74. splitorColor: {
  75. type: String,
  76. default: '#000000'
  77. },
  78. splitorColorClass: {
  79. type: String,
  80. default: ''
  81. },
  82. day: {
  83. type: [Number, String],
  84. default: 0
  85. },
  86. hour: {
  87. type: [Number, String],
  88. default: 0
  89. },
  90. minute: {
  91. type: [Number, String],
  92. default: 0
  93. },
  94. second: {
  95. type: [Number, String],
  96. default: 0
  97. }
  98. },
  99. data() {
  100. return {
  101. timer: null,
  102. d: '00',
  103. h: '00',
  104. i: '00',
  105. s: '00',
  106. leftTime: 0,
  107. seconds: 0
  108. };
  109. },
  110. mounted: function(e) {
  111. this.seconds = this.toSeconds(this.day, this.hour, this.minute, this.second);
  112. this.countDown();
  113. this.timer = setInterval(() => {
  114. this.seconds--;
  115. if (this.seconds < 0) {
  116. this.timeUp();
  117. return;
  118. }
  119. this.countDown();
  120. }, 1000);
  121. },
  122. watch: {
  123. day: function(newVal) {
  124. this.timeUp();
  125. this.seconds = this.toSeconds(this.day, this.hour, this.minute, this.second);
  126. this.countDown();
  127. this.timer = setInterval(() => {
  128. this.seconds--;
  129. if (this.seconds < 0) {
  130. this.timeUp();
  131. return;
  132. }
  133. this.countDown();
  134. }, 1000);
  135. },
  136. hour: function(newVal) {
  137. this.timeUp();
  138. this.seconds = this.toSeconds(this.day, this.hour, this.minute, this.second);
  139. this.countDown();
  140. this.timer = setInterval(() => {
  141. this.seconds--;
  142. if (this.seconds < 0) {
  143. this.timeUp();
  144. return;
  145. }
  146. this.countDown();
  147. }, 1000);
  148. },
  149. minute: function(newVal) {
  150. this.timeUp();
  151. this.seconds = this.toSeconds(this.day, this.hour, this.minute, this.second);
  152. this.countDown();
  153. this.timer = setInterval(() => {
  154. this.seconds--;
  155. if (this.seconds < 0) {
  156. this.timeUp();
  157. return;
  158. }
  159. this.countDown();
  160. }, 1000);
  161. },
  162. second: function(newVal) {
  163. this.timeUp();
  164. this.seconds = this.toSeconds(this.day, this.hour, this.minute, this.second);
  165. this.countDown();
  166. this.timer = setInterval(() => {
  167. this.seconds--;
  168. if (this.seconds < 0) {
  169. this.timeUp();
  170. return;
  171. }
  172. this.countDown();
  173. }, 1000);
  174. }
  175. },
  176. beforeDestroy() {
  177. clearInterval(this.timer);
  178. },
  179. methods: {
  180. toSeconds(day, hours, minutes, seconds) {
  181. day = Number(day);
  182. hours = Number(hours);
  183. minutes = Number(minutes);
  184. seconds = Number(seconds);
  185. return day * 60 * 60 * 24 + hours * 60 * 60 + minutes * 60 + seconds;
  186. },
  187. timeUp() {
  188. clearInterval(this.timer);
  189. this.$emit('timeup');
  190. },
  191. countDown() {
  192. let seconds = this.seconds;
  193. let [day, hour, minute, second] = [0, 0, 0, 0];
  194. if (seconds > 0) {
  195. day = Math.floor(seconds / (60 * 60 * 24));
  196. hour = Math.floor(seconds / (60 * 60)) - day * 24;
  197. minute = Math.floor(seconds / 60) - day * 24 * 60 - hour * 60;
  198. second = Math.floor(seconds) - day * 24 * 60 * 60 - hour * 60 * 60 - minute * 60;
  199. } else {
  200. this.timeUp();
  201. }
  202. if (day < 10) {
  203. day = '0' + day;
  204. }
  205. if (hour < 10) {
  206. hour = '0' + hour;
  207. }
  208. if (minute < 10) {
  209. minute = '0' + minute;
  210. }
  211. if (second < 10) {
  212. second = '0' + second;
  213. }
  214. this.d = day;
  215. this.h = hour;
  216. this.i = minute;
  217. this.s = second;
  218. }
  219. }
  220. };
  221. </script>
  222. <style lang="scss">
  223. @charset "UTF-8";
  224. .uni-countdown {
  225. padding: 2rpx 0;
  226. display: inline-flex;
  227. flex-wrap: nowrap;
  228. justify-content: center;
  229. }
  230. .uni-countdown__splitor {
  231. justify-content: center;
  232. line-height: 44rpx;
  233. padding: 0 5rpx;
  234. font-size: 24rpx;
  235. }
  236. .uni-countdown__splitor.day {
  237. line-height: 50rpx;
  238. }
  239. .uni-countdown__number {
  240. line-height: 44rpx;
  241. justify-content: center;
  242. height: 44rpx;
  243. border-radius: 6rpx;
  244. margin: 0 5rpx;
  245. font-size: 28rpx;
  246. border: 1px solid #000;
  247. font-size: 24rpx;
  248. padding: 0 10rpx;
  249. }
  250. </style>