index.vue 3.1 KB

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