uni-countdown.vue 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  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. clearInterval(this.timer)
  74. this.startData();
  75. },
  76. beforeDestroy() {
  77. clearInterval(this.timer)
  78. },
  79. methods: {
  80. toSeconds(day, hours, minutes, seconds) {
  81. return day * 60 * 60 * 24 + hours * 60 * 60 + minutes * 60 + seconds
  82. },
  83. timeUp() {
  84. clearInterval(this.timer)
  85. this.$emit('timeup')
  86. },
  87. countDown() {
  88. let seconds = this.seconds
  89. let [day, hour, minute, second] = [0, 0, 0, 0]
  90. if (seconds > 0) {
  91. day = Math.floor(seconds / (60 * 60 * 24))
  92. hour = Math.floor(seconds / (60 * 60)) - (day * 24)
  93. minute = Math.floor(seconds / 60) - (day * 24 * 60) - (hour * 60)
  94. second = Math.floor(seconds) - (day * 24 * 60 * 60) - (hour * 60 * 60) - (minute * 60)
  95. } else {
  96. this.timeUp()
  97. }
  98. hour = hour + (day*24)
  99. if (day < 10) {
  100. day = '0' + day
  101. }
  102. if (hour < 10) {
  103. hour = '0' + hour
  104. }
  105. if (minute < 10) {
  106. minute = '0' + minute
  107. }
  108. if (second < 10) {
  109. second = '0' + second
  110. }
  111. this.d = day
  112. this.h = hour
  113. this.i = minute
  114. this.s = second
  115. },
  116. startData() {
  117. this.seconds = this.toSeconds(this.day, this.hour, this.minute, this.second)
  118. if(this.timer) {
  119. clearInterval(this.timer)
  120. }
  121. if (this.seconds <= 0) {
  122. return
  123. }
  124. this.countDown()
  125. this.timer = setInterval(() => {
  126. this.seconds--
  127. if (this.seconds < 0) {
  128. this.timeUp()
  129. return
  130. }
  131. this.countDown()
  132. }, 1000)
  133. },
  134. changeFlag() {
  135. if (!this.syncFlag) {
  136. this.seconds = this.toSeconds(this.day, this.hour, this.minute, this.second)
  137. this.startData();
  138. this.syncFlag = true;
  139. }
  140. }
  141. }
  142. }
  143. </script>
  144. <style lang="scss" scoped>
  145. @import '~@/uni.scss';
  146. $countdown-height: 80rpx;
  147. $countdown-width:80rpx;
  148. .uni-countdown {
  149. /* #ifndef APP-NVUE */
  150. display: flex;
  151. /* #endif */
  152. flex-direction: row;
  153. justify-content: flex-start;
  154. position: relative;
  155. top: 5rpx;
  156. left: 15rpx;
  157. }
  158. .uni-countdown__splitor {
  159. /* #ifndef APP-NVUE */
  160. display: flex;
  161. /* #endif */
  162. justify-content: center;
  163. line-height: $countdown-height;
  164. padding: 5rpx 20rpx;
  165. // font-size: $uni-font-size-sm;
  166. font-size: 60rpx;
  167. }
  168. .uni-countdown__number {
  169. /* #ifndef APP-NVUE */
  170. display: flex;
  171. /* #endif */
  172. justify-content: center;
  173. align-items: center;
  174. width: $countdown-width;
  175. height: $countdown-height;
  176. line-height: $countdown-height;
  177. // margin: 5rpx;
  178. text-align: center;
  179. // font-size: $uni-font-size-sm;
  180. font-size: 48rpx;
  181. border-radius: 8rpx;
  182. font-weight: bold;
  183. }
  184. </style>