uni-countdown.vue 3.9 KB

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