vue 封装组件之活动倒计时组件

桃扇骨 2022-12-01 14:10 330阅读 0赞
  1. <template>
  2. <div class="count-down">
  3. <span class="count-down-end-time">{ { endHours}}点场</span>
  4. <span class="count-down-surplus">{ { surplus | filterTime}}</span>
  5. </div>
  6. </template>
  7. <script>
  8. export default {
  9. props: {
  10. // 接收父组件传入的时间
  11. endHours: {
  12. type: Number,
  13. required: true,
  14. default: 0
  15. }
  16. },
  17. data() {
  18. return {
  19. surplus: '',
  20. diffSeconds: 0,
  21. interval: undefined
  22. }
  23. },
  24. methods: {
  25. computedSurplusTime() {
  26. if (this.interval) {
  27. clearInterval(this.interval)
  28. }
  29. const date = new Date()
  30. if (date.getHours() > this.endHours) {
  31. this.surplus = '活动已结束'
  32. return
  33. }
  34. if (date.getHours() === this.endHours) {
  35. this.surplus = '活动进行中'
  36. return
  37. }
  38. if (date.getHours() < this.endHours) {
  39. this.surplus = '活动未开始'
  40. }
  41. const diffHours = this.endHours - date.getHours() - 1
  42. const diffMinutes = 60 - date.getMinutes() - 1
  43. const diffSeconds = 60 - date.getSeconds()
  44. this.diffSeconds = diffHours * 3600 + diffMinutes * 60 + diffSeconds
  45. this.interval = setInterval(() => {
  46. this.diffSeconds -= 1
  47. }, 1000)
  48. }
  49. },
  50. watch: {
  51. diffSeconds(newV) {
  52. const hours = Math.floor(newV / 3600)
  53. const minutes = Math.floor(newV / 60) % 60
  54. const seconds = newV % 60
  55. this.surplus = hours + ':' + minutes + ':' + seconds
  56. if (newV === 0) {
  57. this.computedSurplusTime()
  58. }
  59. },
  60. endHours() {
  61. this.computedSurplusTime()
  62. }
  63. },
  64. created() {
  65. this.computedSurplusTime()
  66. }
  67. }
  68. </script>
  69. <style lang="scss" scoped>
  70. @import '@css/style.scss';
  71. .count-down {
  72. display: inline-block;
  73. font-size: px2rem(14);
  74. margin-left: px2rem(8);
  75. span {
  76. display: inline-block;
  77. padding: px2rem(2) px2rem(4);
  78. }
  79. &-end-time {
  80. background-color: #d81e06;
  81. border-top-left-radius: px2rwm(4);
  82. border-bottom-left-radius: px2rwm(4);
  83. border: px2rem(1) solid #d81e06;
  84. }
  85. &-surplus {
  86. background-color: #fff;
  87. border-top-right-radius: px2rem(4);
  88. border-bottom-right-radius: px2rem(4);
  89. border: px2rem(1) solid #d81e06;
  90. color: #d81e06;
  91. }
  92. }
  93. </style>

filters.js

(main.js中引入,即可使用)

  1. import Vue from 'vue'
  2. Vue.filter('filterTime', function(value) {
  3. if (!value) {
  4. return ''
  5. }
  6. if(value.indexOf(':') === -1) {
  7. return value
  8. }
  9. let result = ''
  10. const arr = value.split(':')
  11. if (parseInt(arr[0]) < 10) {
  12. result = '0' + arr[0]
  13. } else {
  14. result = arr[0]
  15. }
  16. if (parseInt(arr[1]) < 10) {
  17. result = result + ':0' + arr[1]
  18. } else {
  19. result = result + ':' + arr[1]
  20. }
  21. if (parseInt(arr[2]) < 10) {
  22. result = result + ':0' + arr[2]
  23. } else {
  24. result = result + ':' + arr[2]
  25. }
  26. return result
  27. })

使用

  1. <count-down :endHours="9"></count-down>

在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

发表评论

表情:
评论列表 (有 0 条评论,330人围观)

还没有评论,来说两句吧...

相关阅读