CountDown.vue 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. <template>
  2. <view class="div common-count-down">
  3. <text class="span" v-if="!styletype"><text class="span" :class="{'time':ifTimeBlock}">{{ day }}</text>&nbsp;天&nbsp;</text><text class="span" :class="{'time':ifTimeBlock}">{{ hours }}</text><text class="span" v-if="!styletype">&nbsp;时&nbsp;</text><text class="span" v-else>:</text><text class="span" :class="{'time':ifTimeBlock}">{{ minute }}</text><text class="span" v-if="!styletype">&nbsp;分&nbsp;</text><text class="span" v-else>:</text><text class="span" :class="{'time':ifTimeBlock}">{{ second }}</text><text class="span" v-if="!styletype">&nbsp;秒</text>
  4. </view>
  5. </template>
  6. <script>
  7. export default {
  8. name: 'CountDown',
  9. data () {
  10. return {
  11. flag: false,
  12. day: '',
  13. hours: '',
  14. minute: '',
  15. second: '',
  16. time: ''
  17. }
  18. },
  19. props: {
  20. timesurplus: {},
  21. timelimithour: {},
  22. timetype: {},
  23. styletype:{},
  24. ifTimeBlock:{}
  25. },
  26. mounted () {
  27. this.time = setInterval(() => {
  28. if (this.flag == true) {
  29. clearInterval(this.time)
  30. } else {
  31. this.timeDown()
  32. }
  33. }, 1000)
  34. },
  35. methods: {
  36. timeDown () {
  37. let leftTime = 0
  38. if (this.timetype == 1) {
  39. leftTime = parseInt(this.timesurplus / 1000)
  40. } else if (this.timetype == 2) {
  41. leftTime = parseInt((this.timesurplus * 1000 + this.timelimithour * 60 * 60 * 1000 - new Date().getTime()) / 1000)
  42. }
  43. if(!this.styletype){
  44. this.day = parseInt(leftTime / (24 * 60 * 60))
  45. }
  46. this.hours = this.formate(parseInt((leftTime / (60 * 60)) % 24))
  47. this.minute = this.formate(parseInt((leftTime / 60) % 60))
  48. this.second = this.formate(parseInt(leftTime % 60))
  49. if (leftTime <= 0) {
  50. this.flag = true
  51. this.$emit('time-end')
  52. }
  53. },
  54. /*
  55. * 格式化时间
  56. */
  57. formate (time) {
  58. if (time >= 10) {
  59. return time
  60. } else {
  61. return `0${time}`
  62. }
  63. }
  64. }
  65. }
  66. </script>
  67. <style scoped lang='scss'>
  68. .common-count-down{
  69. font-size:$fontSize;color:#999;
  70. }
  71. .common-count-down .time {
  72. display: inline-block;
  73. width: 16px;
  74. height: 16px;
  75. border-radius: 2px;
  76. background-color: #303133;
  77. color: #FFFFFF;
  78. text-align: center;
  79. margin: 0 3px;
  80. }
  81. </style>