uni-countdown.vue 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  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. /**
  15. * Countdown 倒计时
  16. * @description 倒计时组件
  17. * @tutorial https://ext.dcloud.net.cn/plugin?id=25
  18. * @property {String} backgroundColor 背景色
  19. * @property {String} color 文字颜色
  20. * @property {Number} day 天数
  21. * @property {Number} hour 小时
  22. * @property {Number} minute 分钟
  23. * @property {Number} second 秒
  24. * @property {Boolean} showDay = [true|false] 是否显示天数
  25. * @property {Boolean} showColon = [true|false] 是否以冒号为分隔符
  26. * @property {String} splitorColor 分割符号颜色
  27. * @event {Function} timeup 倒计时时间到触发事件
  28. * @example <uni-countdown :day="1" :hour="1" :minute="12" :second="40"></uni-countdown>
  29. */
  30. export default {
  31. name: 'UniCountdown',
  32. props: {
  33. showDay: {
  34. type: Boolean,
  35. default: true
  36. },
  37. showColon: {
  38. type: Boolean,
  39. default: true
  40. },
  41. backgroundColor: {
  42. type: String,
  43. default: '#FFFFFF'
  44. },
  45. borderColor: {
  46. type: String,
  47. default: '#000000'
  48. },
  49. color: {
  50. type: String,
  51. default: '#000000'
  52. },
  53. splitorColor: {
  54. type: String,
  55. default: '#000000'
  56. },
  57. day: {
  58. type: Number,
  59. default: 0
  60. },
  61. hour: {
  62. type: Number,
  63. default: 0
  64. },
  65. minute: {
  66. type: Number,
  67. default: 0
  68. },
  69. second: {
  70. type: Number,
  71. default: 0
  72. }
  73. },
  74. data() {
  75. return {
  76. timer: null,
  77. syncFlag: false,
  78. d: '00',
  79. h: '00',
  80. i: '00',
  81. s: '00',
  82. leftTime: 0,
  83. seconds: 0
  84. }
  85. },
  86. watch: {
  87. day(val) {
  88. this.changeFlag()
  89. },
  90. hour(val) {
  91. this.changeFlag()
  92. },
  93. minute(val) {
  94. this.changeFlag()
  95. },
  96. second(val) {
  97. this.changeFlag()
  98. }
  99. },
  100. created: function(e) {
  101. this.startData();
  102. },
  103. beforeDestroy() {
  104. clearInterval(this.timer)
  105. },
  106. methods: {
  107. toSeconds(day, hours, minutes, seconds) {
  108. return day * 60 * 60 * 24 + hours * 60 * 60 + minutes * 60 + seconds
  109. },
  110. timeUp() {
  111. clearInterval(this.timer)
  112. this.$emit('timeup')
  113. },
  114. countDown() {
  115. let seconds = this.seconds
  116. let [day, hour, minute, second] = [0, 0, 0, 0]
  117. if (seconds > 0) {
  118. day = Math.floor(seconds / (60 * 60 * 24))
  119. hour = Math.floor(seconds / (60 * 60)) - (day * 24)
  120. minute = Math.floor(seconds / 60) - (day * 24 * 60) - (hour * 60)
  121. second = Math.floor(seconds) - (day * 24 * 60 * 60) - (hour * 60 * 60) - (minute * 60)
  122. } else {
  123. this.timeUp()
  124. }
  125. if (day < 10) {
  126. day = '0' + day
  127. }
  128. if (hour < 10) {
  129. hour = '0' + hour
  130. }
  131. if (minute < 10) {
  132. minute = '0' + minute
  133. }
  134. if (second < 10) {
  135. second = '0' + second
  136. }
  137. this.d = day
  138. this.h = hour
  139. this.i = minute
  140. this.s = second
  141. },
  142. startData() {
  143. this.seconds = this.toSeconds(this.day, this.hour, this.minute, this.second)
  144. if (this.seconds <= 0) {
  145. return
  146. }
  147. this.countDown()
  148. this.timer = setInterval(() => {
  149. this.seconds--
  150. if (this.seconds < 0) {
  151. this.timeUp()
  152. return
  153. }
  154. this.countDown()
  155. }, 1000)
  156. },
  157. changeFlag() {
  158. if (!this.syncFlag) {
  159. this.seconds = this.toSeconds(this.day, this.hour, this.minute, this.second)
  160. this.startData();
  161. this.syncFlag = true;
  162. }
  163. }
  164. }
  165. }
  166. </script>
  167. <style scoped>
  168. .uni-countdown {
  169. /* #ifndef APP-NVUE */
  170. display: flex;
  171. /* #endif */
  172. flex-direction: row;
  173. justify-content: flex-start;
  174. padding: 2rpx 0;
  175. }
  176. .uni-countdown__splitor {
  177. /* #ifndef APP-NVUE */
  178. display: flex;
  179. /* #endif */
  180. justify-content: center;
  181. line-height: 48rpx;
  182. padding: 5rpx;
  183. font-size: 12px;
  184. }
  185. .uni-countdown__number {
  186. /* #ifndef APP-NVUE */
  187. display: flex;
  188. /* #endif */
  189. justify-content: center;
  190. align-items: center;
  191. width: 52rpx;
  192. height: 48rpx;
  193. line-height: 48rpx;
  194. margin: 5rpx;
  195. text-align: center;
  196. font-size: 12px;
  197. }
  198. </style>