CountDown.vue 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. <template>
  2. <div class="time">
  3. {{ tipText }}<span class="styleAll" v-if="isDay === true">{{ day }}</span
  4. ><span class="timeTxt">{{ dayText }}</span
  5. ><span class="styleAll">{{ hour }}</span
  6. ><span class="timeTxt">{{ hourText }}</span
  7. ><span class="styleAll">{{ minute }}</span
  8. ><span class="timeTxt">{{ minuteText }}</span
  9. ><span class="styleAll">{{ second }}</span
  10. ><span class="timeTxt">{{ secondText }}</span>
  11. </div>
  12. </template>
  13. <script>
  14. export default {
  15. name: "CountDown",
  16. props: {
  17. //距离开始提示文字
  18. tipText: {
  19. type: String,
  20. default: "倒计时"
  21. },
  22. dayText: {
  23. type: String,
  24. default: "天"
  25. },
  26. hourText: {
  27. type: String,
  28. default: "时"
  29. },
  30. minuteText: {
  31. type: String,
  32. default: "分"
  33. },
  34. secondText: {
  35. type: String,
  36. default: "秒"
  37. },
  38. datatime: {
  39. type: Number,
  40. default: 0
  41. },
  42. isDay: {
  43. type: Boolean,
  44. default: true
  45. }
  46. },
  47. data: function() {
  48. return {
  49. day: "00",
  50. hour: "00",
  51. minute: "00",
  52. second: "00"
  53. };
  54. },
  55. created: function() {
  56. this.show_time();
  57. },
  58. mounted: function() {},
  59. methods: {
  60. show_time: function() {
  61. let that = this;
  62. function runTime() {
  63. //时间函数
  64. let intDiff = that.datatime - Date.parse(new Date()) / 1000; //获取数据中的时间戳的时间差;
  65. let day = 0,
  66. hour = 0,
  67. minute = 0,
  68. second = 0;
  69. if (intDiff > 0) {
  70. //转换时间
  71. if (that.isDay === true) {
  72. day = Math.floor(intDiff / (60 * 60 * 24));
  73. } else {
  74. day = 0;
  75. }
  76. hour = Math.floor(intDiff / (60 * 60)) - day * 24;
  77. minute = Math.floor(intDiff / 60) - day * 24 * 60 - hour * 60;
  78. second =
  79. Math.floor(intDiff) -
  80. day * 24 * 60 * 60 -
  81. hour * 60 * 60 -
  82. minute * 60;
  83. if (hour <= 9) hour = "0" + hour;
  84. if (minute <= 9) minute = "0" + minute;
  85. if (second <= 9) second = "0" + second;
  86. that.day = day;
  87. that.hour = hour;
  88. that.minute = minute;
  89. that.second = second;
  90. } else {
  91. that.day = "00";
  92. that.hour = "00";
  93. that.minute = "00";
  94. that.second = "00";
  95. }
  96. }
  97. runTime();
  98. setInterval(runTime, 1000);
  99. }
  100. }
  101. };
  102. </script>