use-count-down.vue 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245
  1. <template>
  2. <view class="use-count-down dflex">
  3. <view v-if="showDays" class="use-count-down-item dflex-c" :style="[itemStyle]">
  4. <view class="use-count-down-time" :style="[letterStyle]">
  5. {{ d }}
  6. </view>
  7. </view>
  8. <view v-if="showDays" class="use-count-down-colon dflex-c"
  9. :style="{fontSize: separatorSize + 'rpx', color: separatorColor, paddingBottom: separator == 'colon' ? '4rpx' : 0}">
  10. {{ separator == 'colon' ? ':' : '天' }}
  11. </view>
  12. <view v-if="showHours" class="use-count-down-item dflex-c" :style="[itemStyle]">
  13. <view class="use-count-down-time" :style="{ fontSize: fontSize + 'rpx', color: color}">
  14. {{ h }}
  15. </view>
  16. </view>
  17. <view v-if="showHours" class="use-count-down-colon dflex-c"
  18. :style="{fontSize: separatorSize + 'rpx', color: separatorColor, paddingBottom: separator == 'colon' ? '4rpx' : 0}">
  19. {{ separator == 'colon' ? ':' : '时' }}
  20. </view>
  21. <view v-if="showMinutes" class="use-count-down-item dflex-c" :style="[itemStyle]">
  22. <view class="use-count-down-time" :style="{ fontSize: fontSize + 'rpx', color: color}">
  23. {{ i }}
  24. </view>
  25. </view>
  26. <view v-if="showMinutes" class="use-count-down-colon dflex-c"
  27. :style="{fontSize: separatorSize + 'rpx', color: separatorColor, paddingBottom: separator == 'colon' ? '4rpx' : 0}">
  28. {{ separator == 'colon' ? ':' : '分' }}
  29. </view>
  30. <view v-if="showSeconds" class="use-count-down-item dflex-c" :style="[itemStyle]">
  31. <view class="use-count-down-time" :style="{ fontSize: fontSize + 'rpx', color: color}">
  32. {{ s }}
  33. </view>
  34. </view>
  35. <view v-if="showSeconds && separator == 'zh'" class="use-count-down-colon dflex-c"
  36. :style="{fontSize: separatorSize + 'rpx', color: separatorColor, paddingBottom: separator == 'colon' ? '4rpx' : 0}">
  37. </view>
  38. </view>
  39. </template>
  40. <script>
  41. export default {
  42. props: {
  43. // 倒计时的时间,秒为单位
  44. timestamp: {
  45. type: [Number, String],
  46. default: 0
  47. },
  48. // 是否自动开始倒计时
  49. autoplay: {
  50. type: Boolean,
  51. default: true
  52. },
  53. // 用英文冒号(colon)或者中文(zh)当做分隔符,false的时候为中文,如:"11:22"或"11时22秒"
  54. separator: {
  55. type: String,
  56. default: 'colon'
  57. },
  58. // 分隔符的大小,单位rpx
  59. separatorSize: {
  60. type: [Number, String],
  61. default: 30
  62. },
  63. // 分隔符颜色
  64. separatorColor: {
  65. type: String,
  66. default: "#303133"
  67. },
  68. // 字体颜色
  69. color: {
  70. type: String,
  71. default: '#303133'
  72. },
  73. // 字体大小,单位rpx
  74. fontSize: {
  75. type: [Number, String],
  76. default: 30
  77. },
  78. // 背景颜色
  79. bgColor: {
  80. type: String,
  81. default: '#fff'
  82. },
  83. // 数字框高度,单位rpx
  84. height: {
  85. type: [Number, String],
  86. default: 'auto'
  87. },
  88. // 是否显示数字框
  89. showBorder: {
  90. type: Boolean,
  91. default: false
  92. },
  93. // 边框颜色
  94. borderColor: {
  95. type: String,
  96. default: '#303133'
  97. },
  98. // 是否显示秒
  99. showSeconds: {
  100. type: Boolean,
  101. default: true
  102. },
  103. // 是否显示分钟
  104. showMinutes: {
  105. type: Boolean,
  106. default: true
  107. },
  108. // 是否显示小时
  109. showHours: {
  110. type: Boolean,
  111. default: true
  112. },
  113. // 是否显示“天”
  114. showDays: {
  115. type: Boolean,
  116. default: true
  117. },
  118. },
  119. watch: {
  120. // 监听时间戳的变化
  121. timestamp(newVal, oldVal) {
  122. // 如果倒计时间发生变化,清除定时器,重新开始倒计时
  123. clearInterval(this.timer);
  124. this.start();
  125. }
  126. },
  127. data() {
  128. return {
  129. d: '00', // 天的默认值
  130. h: '00', // 小时的默认值
  131. i: '00', // 分钟的默认值
  132. s: '00', // 秒的默认值
  133. timer: null ,// 定时器
  134. seconds: 0, // 记录不停倒计过程中变化的秒数
  135. };
  136. },
  137. computed: {
  138. // 倒计时item的样式,item为分别的时分秒部分的数字
  139. itemStyle() {
  140. let style = {};
  141. if(this.height) {
  142. style.height = this.height + 'rpx';
  143. style.width = this.height + 'rpx';
  144. }
  145. if(this.showBorder) {
  146. style.borderStyle = 'solid';
  147. style.borderColor = this.borderColor;
  148. style.borderWidth = '1px';
  149. }
  150. if(this.bgColor) {
  151. style.backgroundColor = this.bgColor;
  152. }
  153. return style;
  154. },
  155. // 倒计时数字的样式
  156. letterStyle() {
  157. let style = {};
  158. if(this.fontSize) style.fontSize = this.fontSize + 'rpx';
  159. if(this.color) style.color = this.color;
  160. return style;
  161. }
  162. },
  163. mounted() {
  164. // 如果自动倒计时
  165. this.autoplay && this.timestamp && this.start();
  166. },
  167. methods: {
  168. // 倒计时
  169. start() {
  170. if (this.timestamp <= 0) return;
  171. this.seconds = Number(this.timestamp);
  172. this.formatTime(this.seconds);
  173. this.timer = setInterval(() => {
  174. this.seconds--;
  175. // 发出change事件
  176. this.$emit('change', this.seconds);
  177. if (this.seconds < 0) {
  178. return this.end();
  179. }
  180. this.formatTime(this.seconds);
  181. }, 1000);
  182. },
  183. // 格式化时间
  184. formatTime(seconds) {
  185. // 小于等于0的话,结束倒计时
  186. seconds <= 0 && this.end();
  187. let [day, hour, minute, second] = [0, 0, 0, 0];
  188. day = Math.floor(seconds / (60 * 60 * 24));
  189. // 判断是否显示“天”参数,如果不显示,将天部分的值,加入到小时中
  190. // hour为给后面计算秒和分等用的(基于显示天的前提下计算)
  191. hour = Math.floor(seconds / (60 * 60)) - day * 24;
  192. // showHour为需要显示的小时
  193. let showHour = null;
  194. if(this.showDays) {
  195. showHour = hour;
  196. } else {
  197. showHour = Math.floor(seconds / (60 * 60));
  198. }
  199. minute = Math.floor(seconds / 60) - hour * 60 - day * 24 * 60;
  200. second = Math.floor(seconds) - day * 24 * 60 * 60 - hour * 60 * 60 - minute * 60;
  201. // 如果小于10,在前面补上一个"0"
  202. hour = hour < 10 ? '0' + hour : hour;
  203. minute = minute < 10 ? '0' + minute : minute;
  204. second = second < 10 ? '0' + second : second;
  205. this.d = day;
  206. this.h = showHour;
  207. this.i = minute;
  208. this.s = second;
  209. },
  210. // 停止倒计时
  211. end() {
  212. // 清除定时器
  213. clearInterval(this.timer);
  214. this.timer = null;
  215. this.$emit('end', {});
  216. }
  217. },
  218. beforeDestroy() {
  219. clearInterval(this.timer);
  220. this.timer = null;
  221. }
  222. };
  223. </script>
  224. <style lang="scss">
  225. .use-count-down {}
  226. .use-count-down-item {
  227. padding: 2rpx;
  228. border-radius: 6rpx;
  229. white-space: nowrap;
  230. transform: translateZ(0);
  231. }
  232. .use-count-down-time {}
  233. .use-count-down-colon {
  234. padding: 0 5rpx;
  235. line-height: 1;
  236. padding-bottom: 4rpx;
  237. }
  238. .use-count-down-scale {
  239. transform: scale(0.9);
  240. transform-origin: center center;
  241. }
  242. </style>