uni-countdownold.vue 5.2 KB

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