足力健前端,vue版本

uni-countdown.vue 4.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  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 lang="scss" scoped>
  168. $countdown-height: 48rpx;
  169. $countdown-width: 52rpx;
  170. .uni-countdown {
  171. /* #ifndef APP-NVUE */
  172. display: flex;
  173. /* #endif */
  174. flex-direction: row;
  175. justify-content: flex-start;
  176. padding: 2rpx 0;
  177. }
  178. .uni-countdown__splitor {
  179. /* #ifndef APP-NVUE */
  180. display: flex;
  181. /* #endif */
  182. justify-content: center;
  183. line-height: $countdown-height;
  184. padding: 5rpx;
  185. font-size: $uni-font-size-sm;
  186. }
  187. .uni-countdown__number {
  188. /* #ifndef APP-NVUE */
  189. display: flex;
  190. /* #endif */
  191. justify-content: center;
  192. align-items: center;
  193. width: $countdown-width;
  194. height: $countdown-height;
  195. line-height: $countdown-height;
  196. margin: 5rpx;
  197. text-align: center;
  198. font-size: $uni-font-size-sm;
  199. }
  200. </style>