index.vue 3.7 KB

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