index.vue 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. <template>
  2. <view class="time" :style="justifyLeft">
  3. <!-- 倒计时 -->
  4. <text class="title red" v-if="tipText.trim()">{{ tipText }}</text>
  5. <text class="styleAll" :style="'background-color:' + bgColor + ';color:' + colors + ';'" v-if="isDay === true">
  6. {{ day }}
  7. <text class="dayTxt">{{ inDayText }}</text>
  8. </text>
  9. <text class="timeTxt red" :style="'color:' + dotColor" v-if="dayText">{{ dayText }}</text>
  10. <text class="styleAll" :style="'background-color:' + bgColor + ';color:' + colors + ';'">{{ hour }}</text>
  11. <text class="timeTxt red" :style="'color:' + dotColor" v-if="hourText">{{ hourText }}</text>
  12. <text class="styleAll" :style="'background-color:' + bgColor + ';color:' + colors + ';'">{{ minute }}</text>
  13. <text class="timeTxt red" :style="'color:' + dotColor" v-if="minuteText">{{ minuteText }}</text>
  14. <text class="styleAll" :style="'background-color:' + bgColor + ';color:' + colors + ';'" v-if="isSecond === true">{{ second }}</text>
  15. <text class="timeTxt red" :style="'color:' + dotColor" v-if="secondText">{{ secondText }}</text>
  16. <slot name="bottom"></slot>
  17. </view>
  18. </template>
  19. <script>
  20. export default {
  21. name: 'countDown',
  22. props: {
  23. justifyLeft: {
  24. type: String,
  25. default: ''
  26. },
  27. //距离开始提示文字
  28. tipText: {
  29. type: String,
  30. default: '倒计时'
  31. },
  32. inDayText: {
  33. type: String,
  34. default: '天'
  35. },
  36. dayText: {
  37. type: String,
  38. default: '天'
  39. },
  40. hourText: {
  41. type: String,
  42. default: '时'
  43. },
  44. minuteText: {
  45. type: String,
  46. default: '分'
  47. },
  48. secondText: {
  49. type: String,
  50. default: '秒'
  51. },
  52. datatime: {
  53. type: Number,
  54. default: 0
  55. },
  56. isDay: {
  57. type: Boolean,
  58. default: true
  59. },
  60. isSecond: {
  61. type: Boolean,
  62. default: true
  63. },
  64. bgColor: {
  65. type: String,
  66. default: ''
  67. },
  68. colors: {
  69. type: String,
  70. default: '#e93323'
  71. },
  72. dotColor: {
  73. type: String,
  74. default: '#ffffff'
  75. }
  76. },
  77. data: function () {
  78. return {
  79. day: '00',
  80. hour: '00',
  81. minute: '00',
  82. second: '00',
  83. times: null
  84. };
  85. },
  86. watch:{
  87. datatime:{
  88. handler(val){
  89. this.show_time();
  90. },
  91. immediate: true
  92. }
  93. },
  94. methods: {
  95. show_time: function () {
  96. let that = this;
  97. function runTime() {
  98. //时间函数
  99. let intDiff = that.datatime - Date.parse(new Date()) / 1000; //获取数据中的时间戳的时间差;
  100. let day = 0,
  101. hour = 0,
  102. minute = 0,
  103. second = 0;
  104. if (intDiff > 0) {
  105. //转换时间
  106. if (that.isDay === true) {
  107. day = Math.floor(intDiff / (60 * 60 * 24));
  108. } else {
  109. day = 0;
  110. }
  111. hour = Math.floor(intDiff / (60 * 60)) - day * 24;
  112. minute = Math.floor(intDiff / 60) - day * 24 * 60 - hour * 60;
  113. second = Math.floor(intDiff) - day * 24 * 60 * 60 - hour * 60 * 60 - minute * 60;
  114. if (hour <= 9) hour = '0' + hour;
  115. if (minute <= 9) minute = '0' + minute;
  116. if (second <= 9) second = '0' + second;
  117. that.day = day;
  118. that.hour = hour;
  119. that.minute = minute;
  120. that.second = second;
  121. } else {
  122. that.day = '00';
  123. that.hour = '00';
  124. that.minute = '00';
  125. that.second = '00';
  126. clearInterval(that.times);
  127. that.times = null;
  128. that.$emit('endTime', 1);
  129. }
  130. }
  131. runTime();
  132. this.times = setInterval(runTime, 1000);
  133. }
  134. }
  135. };
  136. </script>
  137. <style>
  138. .time {
  139. display: flex;
  140. justify-content: center;
  141. }
  142. .timeTxt {
  143. margin: 0 4rpx;
  144. }
  145. </style>