graceCountDown.nvue 4.2 KB

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