countdown.js 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. define(['vue'], function (Vue) {
  2. 'use strict';
  3. Vue.component('count-down', {
  4. props: {
  5. start: {
  6. type: String,
  7. default: '倒计时:'
  8. },
  9. dayUnit: {
  10. type: String,
  11. default: '日'
  12. },
  13. horUnit: {
  14. type: String,
  15. default: '时'
  16. },
  17. minUnit: {
  18. type: String,
  19. default: '分'
  20. },
  21. secUnit: {
  22. type: String,
  23. default: '秒'
  24. },
  25. time: {
  26. type: Number,
  27. default: 0
  28. },
  29. dayShow: {
  30. type: Boolean,
  31. default: true
  32. },
  33. secShow: {
  34. type: Boolean,
  35. default: true
  36. }
  37. },
  38. template: '<div class="time">{{start}}' +
  39. '<span v-if="dayShow" class="styleAll">{{day}}{{dayUnit}}</span>' +
  40. '<span class="styleAll">{{hour}}{{horUnit}}</span>' +
  41. '<span class="styleAll">{{minute}}{{minUnit}}</span>' +
  42. '<span v-if="secShow" class="styleAll">{{second}}{{secUnit}}</span>' +
  43. '</div>',
  44. data: function () {
  45. return {
  46. day: "00",
  47. hour: "00",
  48. minute: "00",
  49. second: "00"
  50. }
  51. },
  52. mounted: function () {
  53. this.show_time();
  54. },
  55. methods: {
  56. show_time: function () {
  57. var that = this;
  58. function runTime() {//时间函数
  59. var intDiff = that.time - Date.parse(new Date()) / 1000;//获取数据中的时间戳的时间差;
  60. var day = 0, hour = 0, minute = 0, second = 0;
  61. if (intDiff > 0) {//转换时间
  62. if (that.isDay == true) {
  63. day = Math.floor(intDiff / (60 * 60 * 24));
  64. } else {
  65. day = 0;
  66. }
  67. hour = Math.floor(intDiff / (60 * 60)) - (day * 24);
  68. minute = Math.floor(intDiff / 60) - (day * 24 * 60) - (hour * 60);
  69. second = Math.floor(intDiff) - (day * 24 * 60 * 60) - (hour * 60 * 60) - (minute * 60);
  70. if (hour <= 9) hour = '0' + hour;
  71. if (minute <= 9) minute = '0' + minute;
  72. if (second <= 9) second = '0' + second;
  73. that.day = day;
  74. that.hour = hour;
  75. that.minute = minute;
  76. that.second = second;
  77. } else {
  78. that.day = "00";
  79. that.hour = "00";
  80. that.minute = "00";
  81. that.second = "00";
  82. }
  83. }
  84. runTime();
  85. var timer = setInterval(runTime, 1000);
  86. }
  87. }
  88. });
  89. });