index.vue 2.8 KB

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