u-count-down.vue 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318
  1. <template>
  2. <view class="u-countdown">
  3. <view class="u-countdown-item" :style="[itemStyle]" v-if="showDays && (hideZeroDay || (!hideZeroDay && d != '00'))">
  4. <view class="u-countdown-time" :style="[letterStyle]">
  5. {{ d }}
  6. </view>
  7. </view>
  8. <view
  9. class="u-countdown-colon"
  10. :style="{fontSize: separatorSize + 'rpx', color: separatorColor, paddingBottom: separator == 'colon' ? '4rpx' : 0}"
  11. v-if="showDays && (hideZeroDay || (!hideZeroDay && d != '00'))"
  12. >
  13. {{ separator == 'colon' ? ':' : '天' }}
  14. </view>
  15. <view class="u-countdown-item" :style="[itemStyle]" v-if="showHours">
  16. <view class="u-countdown-time" :style="{ fontSize: fontSize + 'rpx', color: color}">
  17. {{ h }}
  18. </view>
  19. </view>
  20. <view
  21. class="u-countdown-colon"
  22. :style="{fontSize: separatorSize + 'rpx', color: separatorColor, paddingBottom: separator == 'colon' ? '4rpx' : 0}"
  23. v-if="showHours"
  24. >
  25. {{ separator == 'colon' ? ':' : '时' }}
  26. </view>
  27. <view class="u-countdown-item" :style="[itemStyle]" v-if="showMinutes">
  28. <view class="u-countdown-time" :style="{ fontSize: fontSize + 'rpx', color: color}">
  29. {{ i }}
  30. </view>
  31. </view>
  32. <view
  33. class="u-countdown-colon"
  34. :style="{fontSize: separatorSize + 'rpx', color: separatorColor, paddingBottom: separator == 'colon' ? '4rpx' : 0}"
  35. v-if="showMinutes"
  36. >
  37. {{ separator == 'colon' ? ':' : '分' }}
  38. </view>
  39. <view class="u-countdown-item" :style="[itemStyle]" v-if="showSeconds">
  40. <view class="u-countdown-time" :style="{ fontSize: fontSize + 'rpx', color: color}">
  41. {{ s }}
  42. </view>
  43. </view>
  44. <view
  45. class="u-countdown-colon"
  46. :style="{fontSize: separatorSize + 'rpx', color: separatorColor, paddingBottom: separator == 'colon' ? '4rpx' : 0}"
  47. v-if="showSeconds && separator == 'zh'"
  48. >
  49. 秒
  50. </view>
  51. </view>
  52. </template>
  53. <script>
  54. /**
  55. * countDown 倒计时
  56. * @description 该组件一般使用于某个活动的截止时间上,通过数字的变化,给用户明确的时间感受,提示用户进行某一个行为操作。
  57. * @tutorial https://www.uviewui.com/components/countDown.html
  58. * @property {String Number} timestamp 倒计时,单位为秒
  59. * @property {Boolean} autoplay 是否自动开始倒计时,如果为false,需手动调用开始方法。见官网说明(默认true)
  60. * @property {String} separator 分隔符,colon为英文冒号,zh为中文(默认colon)
  61. * @property {String Number} separator-size 分隔符的字体大小,单位rpx(默认30)
  62. * @property {String} separator-color 分隔符的颜色(默认#303133)
  63. * @property {String Number} font-size 倒计时字体大小,单位rpx(默认30)
  64. * @property {Boolean} show-border 是否显示倒计时数字的边框(默认false)
  65. * @property {Boolean} hide-zero-day 当"天"的部分为0时,隐藏该字段 (默认true)
  66. * @property {String} border-color 数字边框的颜色(默认#303133)
  67. * @property {String} bg-color 倒计时数字的背景颜色(默认#ffffff)
  68. * @property {String} color 倒计时数字的颜色(默认#303133)
  69. * @property {String} height 数字高度值(宽度等同此值),设置边框时看情况是否需要设置此值,单位rpx(默认auto)
  70. * @property {Boolean} show-days 是否显示倒计时的"天"部分(默认true)
  71. * @property {Boolean} show-hours 是否显示倒计时的"时"部分(默认true)
  72. * @property {Boolean} show-minutes 是否显示倒计时的"分"部分(默认true)
  73. * @property {Boolean} show-seconds 是否显示倒计时的"秒"部分(默认true)
  74. * @event {Function} end 倒计时结束
  75. * @event {Function} change 每秒触发一次,回调为当前剩余的倒计秒数
  76. * @example <u-count-down ref="uCountDown" :timestamp="86400" :autoplay="false"></u-count-down>
  77. */
  78. export default {
  79. name: 'u-count-down',
  80. props: {
  81. // 倒计时的时间,秒为单位
  82. timestamp: {
  83. type: [Number, String],
  84. default: 0
  85. },
  86. // 是否自动开始倒计时
  87. autoplay: {
  88. type: Boolean,
  89. default: true
  90. },
  91. // 用英文冒号(colon)或者中文(zh)当做分隔符,false的时候为中文,如:"11:22"或"11时22秒"
  92. separator: {
  93. type: String,
  94. default: 'colon'
  95. },
  96. // 分隔符的大小,单位rpx
  97. separatorSize: {
  98. type: [Number, String],
  99. default: 30
  100. },
  101. // 分隔符颜色
  102. separatorColor: {
  103. type: String,
  104. default: "#303133"
  105. },
  106. // 字体颜色
  107. color: {
  108. type: String,
  109. default: '#303133'
  110. },
  111. // 字体大小,单位rpx
  112. fontSize: {
  113. type: [Number, String],
  114. default: 30
  115. },
  116. // 背景颜色
  117. bgColor: {
  118. type: String,
  119. default: '#fff'
  120. },
  121. // 数字框高度,单位rpx
  122. height: {
  123. type: [Number, String],
  124. default: 'auto'
  125. },
  126. // 是否显示数字框
  127. showBorder: {
  128. type: Boolean,
  129. default: false
  130. },
  131. // 边框颜色
  132. borderColor: {
  133. type: String,
  134. default: '#303133'
  135. },
  136. // 是否显示秒
  137. showSeconds: {
  138. type: Boolean,
  139. default: true
  140. },
  141. // 是否显示分钟
  142. showMinutes: {
  143. type: Boolean,
  144. default: true
  145. },
  146. // 是否显示小时
  147. showHours: {
  148. type: Boolean,
  149. default: true
  150. },
  151. // 是否显示“天”
  152. showDays: {
  153. type: Boolean,
  154. default: true
  155. },
  156. // 当"天"的部分为0时,不显示
  157. hideZeroDay: {
  158. type: Boolean,
  159. default: false
  160. }
  161. },
  162. watch: {
  163. // 监听时间戳的变化
  164. timestamp(newVal, oldVal) {
  165. // 如果倒计时间发生变化,清除定时器,重新开始倒计时
  166. this.clearTimer();
  167. this.start();
  168. }
  169. },
  170. data() {
  171. return {
  172. d: '00', // 天的默认值
  173. h: '00', // 小时的默认值
  174. i: '00', // 分钟的默认值
  175. s: '00', // 秒的默认值
  176. timer: null ,// 定时器
  177. seconds: 0, // 记录不停倒计过程中变化的秒数
  178. };
  179. },
  180. computed: {
  181. // 倒计时item的样式,item为分别的时分秒部分的数字
  182. itemStyle() {
  183. let style = {};
  184. if(this.height) {
  185. style.height = this.height + 'rpx';
  186. style.width = this.height + 'rpx';
  187. }
  188. if(this.showBorder) {
  189. style.borderStyle = 'solid';
  190. style.borderColor = this.borderColor;
  191. style.borderWidth = '1px';
  192. }
  193. if(this.bgColor) {
  194. style.backgroundColor = this.bgColor;
  195. }
  196. return style;
  197. },
  198. // 倒计时数字的样式
  199. letterStyle() {
  200. let style = {};
  201. if(this.fontSize) style.fontSize = this.fontSize + 'rpx';
  202. if(this.color) style.color = this.color;
  203. return style;
  204. }
  205. },
  206. mounted() {
  207. // 如果自动倒计时
  208. this.autoplay && this.timestamp && this.start();
  209. },
  210. methods: {
  211. // 倒计时
  212. start() {
  213. // 避免可能出现的倒计时重叠情况
  214. this.clearTimer();
  215. if (this.timestamp <= 0) return;
  216. this.seconds = Number(this.timestamp);
  217. this.formatTime(this.seconds);
  218. this.timer = setInterval(() => {
  219. this.seconds--;
  220. // 发出change事件
  221. this.$emit('change', this.seconds);
  222. if (this.seconds < 0) {
  223. return this.end();
  224. }
  225. this.formatTime(this.seconds);
  226. }, 1000);
  227. },
  228. // 格式化时间
  229. formatTime(seconds) {
  230. // 小于等于0的话,结束倒计时
  231. seconds <= 0 && this.end();
  232. let [day, hour, minute, second] = [0, 0, 0, 0];
  233. day = Math.floor(seconds / (60 * 60 * 24));
  234. // 判断是否显示“天”参数,如果不显示,将天部分的值,加入到小时中
  235. // hour为给后面计算秒和分等用的(基于显示天的前提下计算)
  236. hour = Math.floor(seconds / (60 * 60)) - day * 24;
  237. // showHour为需要显示的小时
  238. let showHour = null;
  239. if(this.showDays) {
  240. showHour = hour;
  241. } else {
  242. // 如果不显示天数,将“天”部分的时间折算到小时中去
  243. showHour = Math.floor(seconds / (60 * 60));
  244. }
  245. minute = Math.floor(seconds / 60) - hour * 60 - day * 24 * 60;
  246. second = Math.floor(seconds) - day * 24 * 60 * 60 - hour * 60 * 60 - minute * 60;
  247. // 如果小于10,在前面补上一个"0"
  248. showHour = showHour < 10 ? '0' + showHour : showHour;
  249. minute = minute < 10 ? '0' + minute : minute;
  250. second = second < 10 ? '0' + second : second;
  251. day = day < 10 ? '0' + day : day;
  252. this.d = day;
  253. this.h = showHour;
  254. this.i = minute;
  255. this.s = second;
  256. },
  257. // 停止倒计时
  258. end() {
  259. this.clearTimer();
  260. this.$emit('end', {});
  261. },
  262. // 清除定时器
  263. clearTimer() {
  264. if(this.timer) {
  265. // 清除定时器
  266. clearInterval(this.timer);
  267. this.timer = null;
  268. }
  269. }
  270. },
  271. beforeDestroy() {
  272. clearInterval(this.timer);
  273. this.timer = null;
  274. }
  275. };
  276. </script>
  277. <style scoped lang="scss">
  278. @import "../../libs/css/style.components.scss";
  279. .u-countdown {
  280. /* #ifndef APP-NVUE */
  281. display: inline-flex;
  282. /* #endif */
  283. align-items: center;
  284. }
  285. .u-countdown-item {
  286. @include vue-flex;
  287. align-items: center;
  288. justify-content: center;
  289. padding: 2rpx;
  290. border-radius: 6rpx;
  291. white-space: nowrap;
  292. transform: translateZ(0);
  293. }
  294. .u-countdown-time {
  295. margin: 0;
  296. padding: 0;
  297. line-height: 1;
  298. }
  299. .u-countdown-colon {
  300. @include vue-flex;
  301. justify-content: center;
  302. padding: 0 5rpx;
  303. line-height: 1;
  304. align-items: center;
  305. padding-bottom: 4rpx;
  306. }
  307. .u-countdown-scale {
  308. transform: scale(0.9);
  309. transform-origin: center center;
  310. }
  311. </style>