uni-countdown.vue 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  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:0,
  42. hour:0,
  43. minute:0,
  44. second:0
  45. },
  46. data() {
  47. return {
  48. timer: null,
  49. syncFlag: false,
  50. d: '00',
  51. h: '00',
  52. i: '00',
  53. s: '00',
  54. leftTime: 0,
  55. seconds: 0
  56. }
  57. },
  58. watch: {
  59. day(val) {
  60. this.changeFlag()
  61. },
  62. hour(val) {
  63. this.changeFlag()
  64. },
  65. minute(val) {
  66. this.changeFlag()
  67. },
  68. second(val) {
  69. this.changeFlag()
  70. }
  71. },
  72. created: function(e) {
  73. this.startData();
  74. },
  75. beforeDestroy() {
  76. clearInterval(this.timer)
  77. },
  78. methods: {
  79. toSeconds(day, hours, minutes, seconds) {
  80. return day * 60 * 60 * 24 + hours * 60 * 60 + minutes * 60 + seconds
  81. },
  82. timeUp() {
  83. clearInterval(this.timer)
  84. this.$emit('timeup')
  85. },
  86. countDown() {
  87. let seconds = this.seconds
  88. let [day, hour, minute, second] = [0, 0, 0, 0]
  89. if (seconds > 0) {
  90. day = Math.floor(seconds / (60 * 60 * 24))
  91. hour = Math.floor(seconds / (60 * 60)) - (day * 24)
  92. minute = Math.floor(seconds / 60) - (day * 24 * 60) - (hour * 60)
  93. second = Math.floor(seconds) - (day * 24 * 60 * 60) - (hour * 60 * 60) - (minute * 60)
  94. } else {
  95. this.timeUp()
  96. }
  97. if (day < 10) {
  98. day = '0' + day
  99. }
  100. if (hour < 10) {
  101. hour = '0' + hour
  102. }
  103. if (minute < 10) {
  104. minute = '0' + minute
  105. }
  106. if (second < 10) {
  107. second = '0' + second
  108. }
  109. this.d = day
  110. this.h = hour
  111. this.i = minute
  112. this.s = second
  113. },
  114. startData() {
  115. this.seconds = this.toSeconds(this.day, this.hour, this.minute, this.second)
  116. if (this.seconds <= 0) {
  117. return
  118. }
  119. this.countDown()
  120. this.timer = setInterval(() => {
  121. this.seconds--
  122. if (this.seconds < 0) {
  123. this.timeUp()
  124. return
  125. }
  126. this.countDown()
  127. }, 1000)
  128. },
  129. changeFlag() {
  130. if (!this.syncFlag) {
  131. this.seconds = this.toSeconds(this.day, this.hour, this.minute, this.second)
  132. this.startData();
  133. this.syncFlag = true;
  134. }
  135. }
  136. }
  137. }
  138. </script>
  139. <style lang="scss" scoped>
  140. @import '~@/uni.scss';
  141. $countdown-height: 40rpx;
  142. $countdown-width: 40rpx;
  143. .uni-countdown {
  144. /* #ifndef APP-NVUE */
  145. display: flex;
  146. /* #endif */
  147. flex-direction: row;
  148. justify-content: flex-start;
  149. position: relative;
  150. top: 5rpx;
  151. left: 15rpx;
  152. }
  153. .uni-countdown__splitor {
  154. /* #ifndef APP-NVUE */
  155. display: flex;
  156. /* #endif */
  157. justify-content: center;
  158. line-height: $countdown-height;
  159. padding: 5rpx;
  160. font-size: $uni-font-size-sm;
  161. }
  162. .uni-countdown__number {
  163. /* #ifndef APP-NVUE */
  164. display: flex;
  165. /* #endif */
  166. justify-content: center;
  167. align-items: center;
  168. width: $countdown-width;
  169. height: $countdown-height;
  170. line-height: $countdown-height;
  171. // margin: 5rpx;
  172. text-align: center;
  173. font-size: $uni-font-size-sm;
  174. }
  175. </style>