countdown.vue 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  1. <template>
  2. <view>
  3. <view class="countdown" v-if="is_show && !is_end">
  4. <block v-if="propMsecShow">
  5. <view class="time" :style="time_style">{{msec}}</view>
  6. <view class="ds" :style="ds_style">{{propSecondDs}}</view>
  7. </block>
  8. <view class="time" :style="time_style">{{second}}</view>
  9. <view class="ds" :style="ds_style">{{propMinuteDs}}</view>
  10. <view class="time" :style="time_style">{{minute}}</view>
  11. <view class="ds" :style="ds_style">{{propHourDs}}</view>
  12. <view class="time" :style="time_style">{{hour}}</view>
  13. </view>
  14. <view v-if="is_show && is_end" class="timer-title">{{propMsg}}</view>
  15. </view>
  16. </template>
  17. <script>
  18. export default {
  19. data() {
  20. return {
  21. hour: '00',
  22. minute: '00',
  23. second: '00',
  24. msec: 0,
  25. is_show: true,
  26. is_end: false,
  27. timer: null,
  28. timers: null,
  29. time_style: '',
  30. ds_style: '',
  31. };
  32. },
  33. components: {},
  34. props: {
  35. propHour: {
  36. type: [String,Number],
  37. default: '00'
  38. },
  39. propMinute: {
  40. type: [String,Number],
  41. default: '00'
  42. },
  43. propSecond: {
  44. type: [String,Number],
  45. default: '00'
  46. },
  47. propEndShow: {
  48. type: Boolean,
  49. default: false
  50. },
  51. propMsecShow: {
  52. type: Boolean,
  53. default: false
  54. },
  55. propMsg: {
  56. type: String,
  57. default: '已结束'
  58. },
  59. propHourDs: {
  60. type: String,
  61. default: ':'
  62. },
  63. propMinuteDs: {
  64. type: String,
  65. default: ':'
  66. },
  67. propSecondDs: {
  68. type: String,
  69. default: ':'
  70. },
  71. propTimePadding: {
  72. type: [Number,String],
  73. default: 8
  74. },
  75. propTimeSize: {
  76. type: [Number,String],
  77. default: 24
  78. },
  79. propTimeBackgroundColor: {
  80. type: String,
  81. default: '#F80001'
  82. },
  83. propTimeColor: {
  84. type: String,
  85. default: '#FFF'
  86. },
  87. propTimeWeight: {
  88. type: [Number,String],
  89. default: '400'
  90. },
  91. propDsColor: {
  92. type: String,
  93. default: '#4B5459'
  94. },
  95. propDsSize: {
  96. type: [Number,String],
  97. default: 24
  98. },
  99. propDsWeight: {
  100. type: [Number,String],
  101. default: '400'
  102. }
  103. },
  104. created: function(e) {
  105. // 样式拼接
  106. this.time_style = 'padding: 0 '+this.propTimePadding+'rpx;background-color:'+this.propTimeBackgroundColor+';color:'+this.propTimeColor+';font-size:'+this.propTimeSize+'rpx;font-weight:'+this.propTimeWeight;
  107. this.ds_style = 'color:'+this.propDsColor+';font-size:'+this.propDsSize+'rpx;font-weight:'+this.propDsWeight;
  108. // 参数处理
  109. this.hour = this.propHour;
  110. this.minute = this.propMinute;
  111. this.second = this.propSecond;
  112. // 定时处理
  113. this.countdown()
  114. },
  115. // #ifndef VUE2
  116. destroyed() {
  117. clearInterval(this.timer);
  118. clearInterval(this.timers);
  119. },
  120. // #endif
  121. // #ifdef VUE3
  122. unmounted() {
  123. clearInterval(this.timer);
  124. clearInterval(this.timers);
  125. },
  126. // #endif
  127. methods: {
  128. // 倒计时处理
  129. countdown() {
  130. // 销毁之前的任务
  131. clearInterval(this.timer);
  132. clearInterval(this.timers);
  133. // 秒
  134. var self = this;
  135. var hour = parseInt(self.hour);
  136. var minute = parseInt(self.minute);
  137. var second = parseInt(self.second);
  138. self.timer = setInterval(function() {
  139. if (second <= 0) {
  140. if (minute > 0) {
  141. minute--;
  142. second = 59;
  143. } else if (hour > 0) {
  144. hour--;
  145. minute = 59;
  146. second = 59;
  147. }
  148. } else {
  149. second--;
  150. }
  151. self.hour = hour < 10 ? '0' + hour : hour;
  152. self.minute = minute < 10 ? '0' + minute : minute;
  153. self.second = second < 10 ? '0' + second : second;
  154. if(self.propHour <= 0 && self.propMinute <= 0 && self.propSecond <= 0) {
  155. // 停止时间
  156. clearInterval(self.timer);
  157. clearInterval(self.timers);
  158. self.is_end = true;
  159. // 活动已结束、是否结束还展示
  160. if(!self.propEndShow) {
  161. self.is_show = false;
  162. }
  163. }
  164. }, 1000);
  165. // 毫秒
  166. var count = 0;
  167. self.timers = setInterval(function() {
  168. count++;
  169. self.msec = count;
  170. if(count > 9) {
  171. count = 0;
  172. }
  173. if(!self.is_show) {
  174. clearInterval(self.timers);
  175. }
  176. }, 100);
  177. }
  178. }
  179. };
  180. </script>
  181. <style>
  182. .countdown {
  183. line-height: 38rpx;
  184. }
  185. .countdown view {
  186. float: right;
  187. }
  188. .countdown .timer-title {
  189. color: #666;
  190. margin-right: 10rpx;
  191. }
  192. .countdown .time {
  193. padding: 0 8rpx;
  194. -moz-border-radius: 8rpx;
  195. border-radius: 8rpx;
  196. background-color: #F80001;
  197. color: #fff;
  198. min-width: 40rpx;
  199. text-align: center;
  200. }
  201. .countdown .ds {
  202. color: #4B5459;
  203. padding: 0 8rpx;
  204. }
  205. </style>