index.vue 4.4 KB

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