uni-countdown.vue 4.5 KB

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