uni-countdown.vue 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  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: 'black'
  28. },
  29. borderColor: {
  30. type: String,
  31. default: '#000000'
  32. },
  33. color: {
  34. type: String,
  35. default: '#fff'
  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. hour = hour + (day*24)
  98. if (day < 10) {
  99. day = '0' + day
  100. }
  101. if (hour < 10) {
  102. hour = '0' + hour
  103. }
  104. if (minute < 10) {
  105. minute = '0' + minute
  106. }
  107. if (second < 10) {
  108. second = '0' + second
  109. }
  110. this.d = day
  111. this.h = hour
  112. this.i = minute
  113. this.s = second
  114. },
  115. startData() {
  116. this.seconds = this.toSeconds(this.day, this.hour, this.minute, this.second)
  117. if (this.seconds <= 0) {
  118. return
  119. }
  120. this.countDown()
  121. this.timer = setInterval(() => {
  122. this.seconds--
  123. if (this.seconds < 0) {
  124. this.timeUp()
  125. return
  126. }
  127. this.countDown()
  128. }, 1000)
  129. },
  130. changeFlag() {
  131. if (!this.syncFlag) {
  132. this.seconds = this.toSeconds(this.day, this.hour, this.minute, this.second)
  133. console.log(this.seconds)
  134. this.startData();
  135. this.syncFlag = true;
  136. }
  137. }
  138. }
  139. }
  140. </script>
  141. <style lang="scss" scoped>
  142. @import '~@/uni.scss';
  143. $countdown-height: 40rpx;
  144. $countdown-width: 40rpx;
  145. .uni-countdown {
  146. /* #ifndef APP-NVUE */
  147. display: flex;
  148. /* #endif */
  149. flex-direction: row;
  150. justify-content: flex-start;
  151. position: relative;
  152. top: 5rpx;
  153. left: 15rpx;
  154. }
  155. .uni-countdown__splitor {
  156. /* #ifndef APP-NVUE */
  157. display: flex;
  158. /* #endif */
  159. justify-content: center;
  160. line-height: $countdown-height;
  161. padding: 5rpx;
  162. font-size: $uni-font-size-sm;
  163. }
  164. .uni-countdown__number {
  165. /* #ifndef APP-NVUE */
  166. display: flex;
  167. /* #endif */
  168. justify-content: center;
  169. align-items: center;
  170. width: $countdown-width;
  171. height: $countdown-height;
  172. line-height: $countdown-height;
  173. // margin: 5rpx;
  174. text-align: center;
  175. font-size: $uni-font-size-sm;
  176. border-radius: 8rpx;
  177. background-color: #333333;
  178. color: #fff;
  179. }
  180. </style>