uni-countdown.vue 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  1. <template>
  2. <view class="uni-countdown">
  3. <text v-if="showDay" :style="{ borderColor: borderColor, color: color, backgroundColor: backgroundColor }"
  4. class="uni-countdown__number">{{ d }}</text>
  5. <text v-if="showDay" :style="{ color: splitorColor }" class="uni-countdown__splitor">天</text>
  6. <text :style="{ borderColor: borderColor, color: color, backgroundColor: backgroundColor }"
  7. class="uni-countdown__number">{{ h }}</text>
  8. <text :style="{ color: splitorColor }" class="uni-countdown__splitor">{{ showColon ? ':' : '时' }}</text>
  9. <text :style="{ borderColor: borderColor, color: color, backgroundColor: backgroundColor }"
  10. class="uni-countdown__number">{{ i }}</text>
  11. <text :style="{ color: splitorColor }" class="uni-countdown__splitor">{{ showColon ? ':' : '分' }}</text>
  12. <text :style="{ borderColor: borderColor, color: color, backgroundColor: backgroundColor }"
  13. class="uni-countdown__number">{{ s }}</text>
  14. <text v-if="!showColon" :style="{ color: splitorColor }" class="uni-countdown__splitor">秒</text>
  15. </view>
  16. </template>
  17. <script>
  18. export default {
  19. name: 'UniCountdown',
  20. props: {
  21. showDay: {
  22. type: Boolean,
  23. default: true
  24. },
  25. showColon: {
  26. type: Boolean,
  27. default: true
  28. },
  29. backgroundColor: {
  30. type: String,
  31. default: '#FFFFFF'
  32. },
  33. borderColor: {
  34. type: String,
  35. default: '#000000'
  36. },
  37. color: {
  38. type: String,
  39. default: '#000000'
  40. },
  41. splitorColor: {
  42. type: String,
  43. default: '#000000'
  44. },
  45. day: {
  46. type: Number,
  47. default: 0
  48. },
  49. hour: {
  50. type: Number,
  51. default: 0
  52. },
  53. minute: {
  54. type: Number,
  55. default: 0
  56. },
  57. second: {
  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. }, 1000)
  144. },
  145. changeFlag() {
  146. if (!this.syncFlag) {
  147. this.seconds = this.toSeconds(this.day, this.hour, this.minute, this.second)
  148. this.startData();
  149. this.syncFlag = true;
  150. }
  151. }
  152. }
  153. }
  154. </script>
  155. <style lang="scss" scoped>
  156. @import '~@/uni.scss';
  157. $countdown-height: 48rpx;
  158. $countdown-width: 52rpx;
  159. .uni-countdown {
  160. /* #ifndef APP-NVUE */
  161. display: flex;
  162. /* #endif */
  163. flex-direction: row;
  164. justify-content: flex-start;
  165. padding: 2rpx 0;
  166. }
  167. .uni-countdown__splitor {
  168. /* #ifndef APP-NVUE */
  169. display: flex;
  170. /* #endif */
  171. justify-content: center;
  172. line-height: $countdown-height;
  173. padding: 5rpx;
  174. font-size: $uni-font-size-sm;
  175. }
  176. .uni-countdown__number {
  177. /* #ifndef APP-NVUE */
  178. display: flex;
  179. /* #endif */
  180. justify-content: center;
  181. align-items: center;
  182. width: $countdown-width;
  183. height: $countdown-height;
  184. line-height: $countdown-height;
  185. margin: 5rpx;
  186. text-align: center;
  187. font-size: $uni-font-size-sm;
  188. border-radius: 10rpx;
  189. }
  190. </style>