index.vue 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  1. <template>
  2. <view class="time" :style="justifyLeft">
  3. <text class="" v-if="tipText">{{ tipText }}</text>
  4. <text class="styleAll p6" v-if="isDay === true" :style="{background:bgColor.bgColor,color:bgColor.Color}">{{ day }}{{bgColor.isDay?'天':''}}</text>
  5. <text class="timeTxt" v-if="dayText" :style="{width:bgColor.timeTxtwidth,color:bgColor.bgColor}">{{ dayText }}</text>
  6. <text class="styleAll" :class='isCol?"timeCol":""' :style="{background:bgColor.bgColor,color:bgColor.Color,width:bgColor.width}">{{ hour }}</text>
  7. <text class="timeTxt" v-if="hourText" :class='isCol?"whit":""' :style="{width:bgColor.timeTxtwidth,color:bgColor.bgColor}">{{ hourText }}</text>
  8. <text class="styleAll" :class='isCol?"timeCol":""' :style="{background:bgColor.bgColor,color:bgColor.Color,width:bgColor.width}">{{ minute }}</text>
  9. <text class="timeTxt" v-if="minuteText" :class='isCol?"whit":""' :style="{width:bgColor.timeTxtwidth,color:bgColor.bgColor}">{{ minuteText }}</text>
  10. <text class="styleAll" :class='isCol?"timeCol":""' :style="{background:bgColor.bgColor,color:bgColor.Color,width:bgColor.width}">{{ second }}</text>
  11. <text class="timeTxt" 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. isCol: {
  52. type: Boolean,
  53. default: false
  54. },
  55. bgColor: {
  56. type: Object,
  57. default: null
  58. }
  59. },
  60. data: function() {
  61. return {
  62. day: "00",
  63. hour: "00",
  64. minute: "00",
  65. second: "00"
  66. };
  67. },
  68. created: function() {
  69. this.show_time();
  70. },
  71. mounted: function() {},
  72. methods: {
  73. show_time: function() {
  74. let that = this;
  75. function runTime() {
  76. //时间函数
  77. let intDiff = that.datatime - Date.parse(new Date()) / 1000; //获取数据中的时间戳的时间差;
  78. let day = 0,
  79. hour = 0,
  80. minute = 0,
  81. second = 0;
  82. if (intDiff > 0) {
  83. //转换时间
  84. if (that.isDay === true) {
  85. day = Math.floor(intDiff / (60 * 60 * 24));
  86. } else {
  87. day = 0;
  88. }
  89. hour = Math.floor(intDiff / (60 * 60)) - day * 24;
  90. minute = Math.floor(intDiff / 60) - day * 24 * 60 - hour * 60;
  91. second =
  92. Math.floor(intDiff) -
  93. day * 24 * 60 * 60 -
  94. hour * 60 * 60 -
  95. minute * 60;
  96. if (hour <= 9) hour = "0" + hour;
  97. if (minute <= 9) minute = "0" + minute;
  98. if (second <= 9) second = "0" + second;
  99. that.day = day;
  100. that.hour = hour;
  101. that.minute = minute;
  102. that.second = second;
  103. } else {
  104. that.day = "00";
  105. that.hour = "00";
  106. that.minute = "00";
  107. that.second = "00";
  108. }
  109. }
  110. runTime();
  111. setInterval(runTime, 1000);
  112. }
  113. }
  114. };
  115. </script>
  116. <style scoped>
  117. .p6{
  118. padding: 0 8rpx;
  119. }
  120. .styleAll{
  121. /* color: #fff; */
  122. font-size: 24rpx;
  123. height: 36rpx;
  124. line-height: 36rpx;
  125. border-radius: 6rpx;
  126. text-align: center;
  127. /* padding: 0 6rpx; */
  128. }
  129. .timeTxt{
  130. text-align: center;
  131. /* width: 16rpx; */
  132. height: 36rpx;
  133. line-height: 36rpx;
  134. display: inline-block;
  135. }
  136. .whit{
  137. color: #fff !important;
  138. }
  139. .time {
  140. display: flex;
  141. justify-content: center;
  142. }
  143. .red {
  144. color: #fc4141;
  145. margin: 0 4rpx;
  146. }
  147. .timeCol {
  148. /* width: 40rpx;
  149. height: 40rpx;
  150. line-height: 40rpx;
  151. text-align:center;
  152. border-radius: 6px;
  153. background: #fff;
  154. font-size: 24rpx; */
  155. color: #E93323;
  156. }
  157. </style>