uni-countdown.vue 5.2 KB

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