s-count-down.vue 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  1. <template>
  2. <view class="time" :style="justifyLeft">
  3. <text class="" v-if="tipText">{{ tipText }}</text>
  4. <text
  5. class="styleAll p6"
  6. v-if="isDay === true"
  7. :style="{ background: bgColor.bgColor, color: bgColor.Color }"
  8. >{{ day }}{{ bgColor.isDay ? '天' : '' }}</text
  9. >
  10. <text
  11. class="timeTxt"
  12. v-if="dayText"
  13. :style="{ width: bgColor.timeTxtwidth, color: bgColor.bgColor }"
  14. >{{ dayText }}</text
  15. >
  16. <text
  17. class="styleAll"
  18. :class="isCol ? 'timeCol' : ''"
  19. :style="{ background: bgColor.bgColor, color: bgColor.Color, width: bgColor.width }"
  20. >{{ hour }}</text
  21. >
  22. <text
  23. class="timeTxt"
  24. v-if="hourText"
  25. :class="isCol ? 'whit' : ''"
  26. :style="{ width: bgColor.timeTxtwidth, color: bgColor.bgColor }"
  27. >{{ hourText }}</text
  28. >
  29. <text
  30. class="styleAll"
  31. :class="isCol ? 'timeCol' : ''"
  32. :style="{ background: bgColor.bgColor, color: bgColor.Color, width: bgColor.width }"
  33. >{{ minute }}</text
  34. >
  35. <text
  36. class="timeTxt"
  37. v-if="minuteText"
  38. :class="isCol ? 'whit' : ''"
  39. :style="{ width: bgColor.timeTxtwidth, color: bgColor.bgColor }"
  40. >{{ minuteText }}</text
  41. >
  42. <text
  43. class="styleAll"
  44. :class="isCol ? 'timeCol' : ''"
  45. :style="{ background: bgColor.bgColor, color: bgColor.Color, width: bgColor.width }"
  46. >{{ second }}</text
  47. >
  48. <text class="timeTxt" v-if="secondText">{{ secondText }}</text>
  49. </view>
  50. </template>
  51. <script>
  52. export default {
  53. name: 'countDown',
  54. props: {
  55. justifyLeft: {
  56. type: String,
  57. default: '',
  58. },
  59. //距离开始提示文字
  60. tipText: {
  61. type: String,
  62. default: '倒计时',
  63. },
  64. dayText: {
  65. type: String,
  66. default: '天',
  67. },
  68. hourText: {
  69. type: String,
  70. default: '时',
  71. },
  72. minuteText: {
  73. type: String,
  74. default: '分',
  75. },
  76. secondText: {
  77. type: String,
  78. default: '秒',
  79. },
  80. datatime: {
  81. type: Number,
  82. default: 0,
  83. },
  84. isDay: {
  85. type: Boolean,
  86. default: true,
  87. },
  88. isCol: {
  89. type: Boolean,
  90. default: false,
  91. },
  92. bgColor: {
  93. type: Object,
  94. default: null,
  95. },
  96. },
  97. data: function () {
  98. return {
  99. day: '00',
  100. hour: '00',
  101. minute: '00',
  102. second: '00',
  103. };
  104. },
  105. created: function () {
  106. this.show_time();
  107. },
  108. mounted: function () {},
  109. methods: {
  110. show_time: function () {
  111. let that = this;
  112. function runTime() {
  113. //时间函数
  114. let intDiff = that.datatime - Date.parse(new Date()) / 1000; //获取数据中的时间戳的时间差;
  115. let day = 0,
  116. hour = 0,
  117. minute = 0,
  118. second = 0;
  119. if (intDiff > 0) {
  120. //转换时间
  121. if (that.isDay === true) {
  122. day = Math.floor(intDiff / (60 * 60 * 24));
  123. } else {
  124. day = 0;
  125. }
  126. hour = Math.floor(intDiff / (60 * 60)) - day * 24;
  127. minute = Math.floor(intDiff / 60) - day * 24 * 60 - hour * 60;
  128. second = Math.floor(intDiff) - day * 24 * 60 * 60 - hour * 60 * 60 - minute * 60;
  129. if (hour <= 9) hour = '0' + hour;
  130. if (minute <= 9) minute = '0' + minute;
  131. if (second <= 9) second = '0' + second;
  132. that.day = day;
  133. that.hour = hour;
  134. that.minute = minute;
  135. that.second = second;
  136. } else {
  137. that.day = '00';
  138. that.hour = '00';
  139. that.minute = '00';
  140. that.second = '00';
  141. }
  142. }
  143. runTime();
  144. setInterval(runTime, 1000);
  145. },
  146. },
  147. };
  148. </script>
  149. <style scoped>
  150. .p6 {
  151. padding: 0 8rpx;
  152. }
  153. .styleAll {
  154. /* color: #fff; */
  155. font-size: 24rpx;
  156. height: 36rpx;
  157. line-height: 36rpx;
  158. border-radius: 6rpx;
  159. text-align: center;
  160. /* padding: 0 6rpx; */
  161. }
  162. .timeTxt {
  163. text-align: center;
  164. /* width: 16rpx; */
  165. height: 36rpx;
  166. line-height: 36rpx;
  167. display: inline-block;
  168. }
  169. .whit {
  170. color: #fff !important;
  171. }
  172. .time {
  173. display: flex;
  174. justify-content: center;
  175. }
  176. .red {
  177. color: #fc4141;
  178. margin: 0 4rpx;
  179. }
  180. .timeCol {
  181. /* width: 40rpx;
  182. height: 40rpx;
  183. line-height: 40rpx;
  184. text-align:center;
  185. border-radius: 6px;
  186. background: #fff;
  187. font-size: 24rpx; */
  188. color: #e93323;
  189. }
  190. </style>