cz-countdown.vue 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. <template>
  2. <view class="countdown" :class="styleName == 'while' ? 'while' : ''">
  3. {{ fontSize }}
  4. <view class="time" :style="'font-size:' + fontSize +'px;'" v-if="d!=0">{{d <= 9 ? ('0' + d) : d }}</view>
  5. <text class="text" :style="'font-size:' + fontSize +'px;'" v-if="d!=0">天</text>
  6. <view class="time" :style="'font-size:' + fontSize +'px;'" style="margin-left: 9rpx;">{{h<10?'0'+h:h}}</view>
  7. <text class="text">小时</text>
  8. <view class="time" :style="'font-size:' + fontSize +'px;'">{{m<10?'0'+m:m}}</view>
  9. <text class="text">分</text>
  10. <view class="time" :style="'font-size:' + fontSize +'px;'">{{s<10?'0'+s:s}}</view>
  11. <text class="text">秒</text>
  12. </view>
  13. </template>
  14. <script>
  15. export default{
  16. props:{
  17. startTime: {
  18. type: String,
  19. },
  20. endTime:{
  21. type: String,
  22. },
  23. fontSize:{
  24. type:Number,
  25. default:10
  26. },
  27. styleName:{
  28. type : String,
  29. default(){
  30. return "while"
  31. }
  32. }
  33. },
  34. data(){
  35. return{
  36. timer:null,
  37. d:0,
  38. h:0,
  39. m:0,
  40. s:0
  41. }
  42. },
  43. mounted() {
  44. this.time()
  45. },
  46. beforeDestroy(){
  47. clearInterval(this.timer);
  48. this.timer = null;
  49. },
  50. computed:{
  51. sTime(){return this.startTime},
  52. eTime(){return this.endTime}
  53. },
  54. watch:{
  55. endTime(){
  56. clearInterval(this.timer);
  57. this.time()
  58. }
  59. },
  60. methods:{
  61. time(){
  62. let leftTime = this.GetDateDiff(this.sTime,this.eTime)
  63. this.getCountdownTime(leftTime)
  64. },
  65. //计算两个时间差
  66. GetDateDiff(startTime, endTime) {
  67. //将xxxx-xx-xx的时间格式,转换为 xxxx/xx/xx的格式
  68. startTime = startTime.replace(/\-/g, "/");
  69. endTime = endTime.replace(/\-/g, "/");
  70. //将计算间隔类性字符转换为小写
  71. var sTime = new Date(startTime);
  72. var eTime = new Date(endTime);
  73. return parseInt((eTime.getTime() - sTime.getTime()) / 1000);
  74. },
  75. //计算活动结束时间
  76. getCountdownTime(leftTime){
  77. let time = leftTime
  78. if (time>0) {
  79. this.timer = setInterval(() => {
  80. if(time == 0){
  81. clearInterval(this.timer)
  82. this.h = 0
  83. this.m = 0
  84. this.s = 0
  85. }else{
  86. this.d = parseInt(leftTime/3600/24);
  87. this.h = parseInt((time/3600)%24);
  88. this.m = parseInt((time/60)%60);
  89. this.s = parseInt(time%60);
  90. time --
  91. }
  92. },1000)
  93. }
  94. }
  95. }
  96. }
  97. </script>
  98. <style lang="scss" scoped>
  99. .countdown{
  100. display: flex;align-items: center;font-size: 20rpx;color: #fff;
  101. .time{padding: 4rpx;background-color: #606060;border-radius: 4rpx;box-sizing: border-box;}
  102. .text{font-size: 36rpx;font-weight: bolder;margin: 0 4rpx;color: #606060;}
  103. }
  104. .countdown.while{
  105. font-size: 10px;color: #FFFFFF;
  106. .time{background: #db292b;border-radius: 4px;color: #fff;text-align :center;}
  107. .text{font-size: 10px;font-weight: bolder;margin: 0 4rpx;}
  108. }
  109. </style>