uni-countdown.vue 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  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. console.log(val,'day')
  61. this.changeFlag()
  62. },
  63. hour(val) {
  64. this.changeFlag()
  65. },
  66. minute(val) {
  67. this.changeFlag()
  68. },
  69. second(val) {
  70. this.changeFlag()
  71. }
  72. },
  73. created: function(e) {
  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.seconds <= 0) {
  119. return
  120. }
  121. this.countDown()
  122. this.timer = setInterval(() => {
  123. this.seconds--
  124. if (this.seconds < 0) {
  125. this.timeUp()
  126. return
  127. }
  128. this.countDown()
  129. }, 1000)
  130. },
  131. changeFlag() {
  132. if (!this.syncFlag) {
  133. this.seconds = this.toSeconds(this.day, this.hour, this.minute, this.second)
  134. console.log(this.seconds)
  135. this.startData();
  136. this.syncFlag = true;
  137. }
  138. }
  139. }
  140. }
  141. </script>
  142. <style lang="scss" scoped>
  143. @import '~@/uni.scss';
  144. $countdown-height: 40rpx;
  145. $countdown-width: 40rpx;
  146. .uni-countdown {
  147. /* #ifndef APP-NVUE */
  148. display: flex;
  149. /* #endif */
  150. flex-direction: row;
  151. justify-content: flex-start;
  152. position: relative;
  153. top: 5rpx;
  154. left: 15rpx;
  155. }
  156. .uni-countdown__splitor {
  157. /* #ifndef APP-NVUE */
  158. display: flex;
  159. /* #endif */
  160. justify-content: center;
  161. line-height: $countdown-height;
  162. padding: 5rpx;
  163. font-size: $uni-font-size-sm;
  164. }
  165. .uni-countdown__number {
  166. /* #ifndef APP-NVUE */
  167. display: flex;
  168. /* #endif */
  169. justify-content: center;
  170. align-items: center;
  171. width: $countdown-width;
  172. height: $countdown-height;
  173. line-height: $countdown-height;
  174. // margin: 5rpx;
  175. text-align: center;
  176. font-size: $uni-font-size-sm;
  177. border-radius: 8rpx;
  178. }
  179. </style>