uni-countdown.vue 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. <template>
  2. <view class="uni-countdown">
  3. <view v-if="showDay" :style="{borderColor:borderColor, color:color, background:backgroundColor}" class="uni-countdown__number">{{ d }}</view>
  4. <view v-if="showDay" :style="{color:splitorColor}" class="uni-countdown__splitor">天</view>
  5. <view :style="{borderColor:borderColor, color:color, background:backgroundColor}" class="uni-countdown__number">{{ h }}</view>
  6. <view :style="{color:splitorColor}" class="uni-countdown__splitor">{{ showColon ? ':' : '时' }}</view>
  7. <view :style="{borderColor:borderColor, color:color, background:backgroundColor}" class="uni-countdown__number">{{ i }}</view>
  8. <view :style="{color:splitorColor}" class="uni-countdown__splitor">{{ showColon ? ':' : '分' }}</view>
  9. <view :style="{borderColor:borderColor, color:color, background:backgroundColor}" class="uni-countdown__number">{{ s }}</view>
  10. <view v-if="!showColon" :style="{color:splitorColor}" class="uni-countdown__splitor">秒</view>
  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. },
  58. data() {
  59. return {
  60. timer: null,
  61. d: '00',
  62. h: '00',
  63. i: '00',
  64. s: '00',
  65. leftTime: 0,
  66. seconds: 0
  67. }
  68. },
  69. created: function(e) {
  70. this.seconds = this.toSeconds(this.day, this.hour, this.minute, this.second)
  71. this.countDown()
  72. this.timer = setInterval(() => {
  73. this.seconds--
  74. if (this.seconds < 0) {
  75. this.timeUp()
  76. return
  77. }
  78. this.countDown()
  79. }, 1000)
  80. },
  81. beforeDestroy() {
  82. clearInterval(this.timer)
  83. },
  84. methods: {
  85. toSeconds(day, hours, minutes, seconds) {
  86. return (day * 60 * 60 * 24) + (hours * 60 * 60) + (minutes * 60) + seconds
  87. },
  88. timeUp() {
  89. clearInterval(this.timer)
  90. this.$emit('timeup')
  91. },
  92. countDown() {
  93. let seconds = this.seconds
  94. let [day, hour, minute, second] = [0, 0, 0, 0]
  95. if (seconds > 0) {
  96. day = Math.floor(seconds / (60 * 60 * 24))
  97. hour = Math.floor(seconds / (60 * 60)) - (day * 24)
  98. minute = Math.floor(seconds / 60) - (day * 24 * 60) - (hour * 60)
  99. second = Math.floor(seconds) - (day * 24 * 60 * 60) - (hour * 60 * 60) - (minute * 60)
  100. } else {
  101. this.timeUp()
  102. }
  103. if (day < 10) {
  104. day = '0' + day
  105. }
  106. if (hour < 10) {
  107. hour = '0' + hour
  108. }
  109. if (minute < 10) {
  110. minute = '0' + minute
  111. }
  112. if (second < 10) {
  113. second = '0' + second
  114. }
  115. this.d = day
  116. this.h = hour
  117. this.i = minute
  118. this.s = second
  119. }
  120. }
  121. }
  122. </script>
  123. <style>
  124. @charset "UTF-8";
  125. .uni-countdown {
  126. padding: 2upx 0;
  127. display: inline-flex;
  128. flex-wrap: nowrap;
  129. justify-content: center
  130. }
  131. .uni-countdown__splitor {
  132. justify-content: center;
  133. line-height: 44upx;
  134. padding: 0 5upx;
  135. font-size: 28upx
  136. }
  137. .uni-countdown__number {
  138. line-height: 44upx;
  139. justify-content: center;
  140. height: 44upx;
  141. border-radius: 6upx;
  142. margin: 0 5upx;
  143. font-size: 28upx;
  144. border: 1px solid #000;
  145. font-size: 24upx;
  146. padding: 0 10upx
  147. }
  148. </style>