You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 

308 rivejä
8.7 KiB

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