uni-countdown.vue 5.5 KB

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