graceNvueCountDown.vue 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. <template name="graceCountDown">
  2. <view class="grace-countdown" v-if="show">
  3. <text class="grace-countdown-numbers" :style="{borderColor:borderColor, width:width, height:width, fontSize:fontSize, lineHeight:width, color:fontColor, backgroundColor:bgrColor}" v-if="d > 0">{{d}}</text>
  4. <text class="grace-countdown-splitor" :style="{color:splitorColor, lineHeight:width, fontSize:fontSize}" v-if="d > 0">{{splitorText[0]}}</text>
  5. <text class="grace-countdown-numbers" :style="{borderColor:borderColor, width:width, height:width, fontSize:fontSize, lineHeight:width, color:fontColor, backgroundColor:bgrColor}" v-if="(h != '00' || zeroShow)">{{h}}</text>
  6. <text class="grace-countdown-splitor" :style="{color:splitorColor, lineHeight:width, fontSize:fontSize}" v-if="(h != '00' || zeroShow)">{{splitorText[1]}}</text>
  7. <text class="grace-countdown-numbers" :style="{borderColor:borderColor, width:width, height:width, fontSize:fontSize, lineHeight:width, color:fontColor, backgroundColor:bgrColor}">{{i}}</text>
  8. <text class="grace-countdown-splitor" :style="{color:splitorColor, lineHeight:width, fontSize:fontSize}">{{splitorText[2]}}</text>
  9. <text class="grace-countdown-numbers" :style="{borderColor:borderColor, width:width, height:width, fontSize:fontSize, lineHeight:width, color:fontColor, backgroundColor:bgrColor}">{{s}}</text>
  10. <text class="grace-countdown-splitor" :style="{color:splitorColor, lineHeight:width, fontSize:fontSize}">{{splitorText[3]}}</text>
  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. zeroShow:{type:Boolean, default:true}
  53. },
  54. data() {
  55. return {
  56. d : 0,
  57. h : "",
  58. i : "",
  59. s : "",
  60. leftTime : 0,
  61. outTimer : null,
  62. leftTimeNum : 0
  63. }
  64. },
  65. created:function(){
  66. this.runbase();
  67. },
  68. updated:function(){
  69. this.runbase();
  70. },
  71. methods: {
  72. runbase : function(){
  73. var reg = /^([0-9]{4})-([0-9]{2})-([0-9]{2}) ([0-9]{2}):([0-9]{2}):([0-9]{2})$/;
  74. var res = this.timer.match(reg);
  75. if (res == null){ return false; }
  76. var year = parseInt(res[1]);
  77. if (year < 1000) { return false; }
  78. var month = parseInt(res[2]);
  79. var day = parseInt(res[3]);
  80. var h = parseInt(res[4]);
  81. if (h < 0 || h > 24) { return false; }
  82. var i = parseInt(res[5]);
  83. if (i < 0 || i > 60) { return false; }
  84. var s = parseInt(res[6]);
  85. if (s < 0 || s > 60) { return false; }
  86. var leftTime = new Date(year, month - 1, day, h, i, s);
  87. this.leftTime = leftTime;
  88. clearTimeout(this.outTimer);
  89. this.countDown();
  90. },
  91. countDown: function (){
  92. var leftTime = this.leftTime - new Date();
  93. this.leftTimeNum = leftTime;
  94. if (leftTime > 0) {
  95. var day = parseInt(leftTime / (1000 * 60 * 60 * 24));
  96. var hours = parseInt((leftTime % (1000 * 60 * 60 * 24)) / (1000 * 60 * 60));
  97. var minutes = parseInt((leftTime % (1000 * 60 * 60)) / (1000 * 60));
  98. var seconds = parseInt((leftTime % (1000 * 60)) / 1000);
  99. if (hours < 10) { hours = '0' + hours;}
  100. if (minutes < 10) { minutes = '0' + minutes; }
  101. if (seconds < 10) { seconds = '0' + seconds; }
  102. this.h = hours; this.i = minutes; this.s = seconds; this.d = day;
  103. this.outTimer = setTimeout(()=>{this.countDown();}, 1000);
  104. }else{
  105. clearTimeout(this.outTimer);
  106. this.h = '00'; this.i = '00'; this.s = '00'; this.d = 0;
  107. this.$emit('endDo');
  108. }
  109. },
  110. reSetTimer : function(timer){
  111. clearTimeout(this.outTimer);
  112. this.timer = timer;
  113. this.runbase();
  114. },
  115. getTimeRemaining : function(){
  116. if(this.leftTimeNum < 0){return 0;}
  117. return parseInt(this.leftTimeNum / 1000);
  118. }
  119. }
  120. }
  121. </script>
  122. <style scoped>
  123. .grace-countdown{flex-wrap:nowrap; justify-content:center; flex-direction:row;}
  124. .grace-countdown-splitor{justify-content:center; padding:0 5rpx;}
  125. .grace-countdown-numbers{border-radius:8rpx; margin:0 5rpx; text-align:center; border-width:1px; border-style:solid; border-color:#000000; font-size:22rpx;}
  126. </style>