uni-countdowns.vue 4.3 KB

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