graceCountDown.vue 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. <template name="graceCountDown">
  2. <view class="grace-countdown" v-if="show">
  3. <view class="grace-countdown-numbers" :style="{borderColor:borderColor, width:width, height:width, fontSize:fontSize, lineHeight:width, color:fontColor, background:bgrColor}" v-if="d > 0">{{d}}</view>
  4. <view class="grace-countdown-splitor" :style="{color:splitorColor, lineHeight:width, fontSize:fontSize}" v-if="d > 0">{{splitorText[0]}}</view>
  5. <view class="grace-countdown-numbers" :style="{borderColor:borderColor, width:width, height:width, fontSize:fontSize, lineHeight:width, color:fontColor, background:bgrColor}">{{h}}</view>
  6. <view class="grace-countdown-splitor" :style="{color:splitorColor, lineHeight:width, fontSize:fontSize}">{{splitorText[1]}}</view>
  7. <view class="grace-countdown-numbers" :style="{borderColor:borderColor, width:width, height:width, fontSize:fontSize, lineHeight:width, color:fontColor, background:bgrColor}">{{i}}</view>
  8. <view class="grace-countdown-splitor" :style="{color:splitorColor, lineHeight:width, fontSize:fontSize}">{{splitorText[2]}}</view>
  9. <view class="grace-countdown-numbers" :style="{borderColor:borderColor, width:width, height:width, fontSize:fontSize, lineHeight:width, color:fontColor, background:bgrColor}">{{s}}</view>
  10. <view class="grace-countdown-splitor" :style="{color:splitorColor, lineHeight:width, fontSize:fontSize}">{{splitorText[3]}}</view>
  11. </view>
  12. </template>
  13. <script>
  14. export default {
  15. name: "graceCountDown",
  16. props: {
  17. bgrColor: {
  18. type: String,
  19. default: "#FFFFFF"
  20. },
  21. borderColor:{
  22. type:String,
  23. default : "#000000"
  24. },
  25. fontColor: {
  26. type: String,
  27. default: "#000000"
  28. },
  29. fontSize: {
  30. type: String,
  31. default: "22rpx"
  32. },
  33. splitorColor: {
  34. type: String,
  35. default: "#000000"
  36. },
  37. timer:{
  38. type:String,
  39. default:""
  40. },
  41. width : {
  42. type:String,
  43. default:"40rpx"
  44. },
  45. splitorText : {
  46. type : Array,
  47. default : function () {
  48. return [':', ':', ':', '']
  49. }
  50. },
  51. show:{type:Boolean, default:true}
  52. },
  53. data() {
  54. return {
  55. d : 0,
  56. h : "",
  57. i : "",
  58. s : "",
  59. leftTime : 0,
  60. outTimer : null,
  61. timerIn : ''
  62. }
  63. },
  64. created:function(){
  65. this.timerIn = this.timer;
  66. this.runbase();
  67. },
  68. methods: {
  69. runbase : function(){
  70. var reg = /^([0-9]{4})-([0-9]{2})-([0-9]{2}) ([0-9]{2}):([0-9]{2}):([0-9]{2})$/;
  71. var res = this.timerIn.match(reg);
  72. if (res == null){this.outTimer = setTimeout(() => { this.runbase(); }, 1000); return false; }
  73. var year = parseInt(res[1]);
  74. if (year < 1000) { return false; }
  75. var month = parseInt(res[2]);
  76. var day = parseInt(res[3]);
  77. var h = parseInt(res[4]);
  78. if (h < 0 || h > 24) { return false; }
  79. var i = parseInt(res[5]);
  80. if (i < 0 || i > 60) { return false; }
  81. var s = parseInt(res[6]);
  82. if (s < 0 || s > 60) { return false; }
  83. var leftTime = new Date(year, month - 1, day, h, i, s);
  84. this.leftTime = leftTime;
  85. clearTimeout(this.outTimer);
  86. this.countDown();
  87. },
  88. countDown: function (){
  89. var leftTime = this.leftTime - new Date();
  90. if (leftTime > 0) {
  91. var day = parseInt(leftTime / (1000 * 60 * 60 * 24));
  92. var hours = parseInt((leftTime % (1000 * 60 * 60 * 24)) / (1000 * 60 * 60));
  93. var minutes = parseInt((leftTime % (1000 * 60 * 60)) / (1000 * 60));
  94. var seconds = parseInt((leftTime % (1000 * 60)) / 1000);
  95. if (hours < 10) { hours = '0' + hours;}
  96. if (minutes < 10) { minutes = '0' + minutes; }
  97. if (seconds < 10) { seconds = '0' + seconds; }
  98. this.h = hours; this.i = minutes; this.s = seconds; this.d = day;
  99. this.outTimer = setTimeout(()=>{this.countDown();}, 1000);
  100. }else{
  101. clearTimeout(this.outTimer);
  102. this.h = '00'; this.i = '00'; this.s = '00'; this.d = 0;
  103. this.$emit('endDo');
  104. }
  105. },
  106. reSetTimer : function(timer){
  107. clearTimeout(this.outTimer);
  108. this.timerIn = timer;
  109. this.runbase();
  110. }
  111. }
  112. }
  113. </script>
  114. <style scoped>
  115. .grace-countdown{display:flex; flex-wrap:nowrap; justify-content:center;}
  116. .grace-countdown-splitor{justify-content:center; padding:0 5rpx;}
  117. .grace-countdown-numbers{border-radius:8rpx; margin:0 5rpx; text-align:center; border:1px solid #000000; font-size:22rpx;}
  118. </style>