index.vue 3.4 KB

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