uni-count-down.vue 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  1. <template>
  2. <view class="uni-countdown">
  3. <text v-if="showDay" :style="{ borderColor: borderColor, color: color, backgroundColor: backgroundColor }" class="uni-countdown__number">{{ d }}</text>
  4. <text v-if="showDay" :style="{ color: splitorColor }" class="uni-countdown__splitor">天</text>
  5. <text :style="{ borderColor: borderColor, color: color, backgroundColor: backgroundColor }" class="uni-countdown__number">{{ h }}</text>
  6. <text :style="{ color: splitorColor }" class="uni-countdown__splitor">{{ showColon ? ':' : '时' }}</text>
  7. <text :style="{ borderColor: borderColor, color: color, backgroundColor: backgroundColor }" class="uni-countdown__number">{{ i }}</text>
  8. <text :style="{ color: splitorColor }" class="uni-countdown__splitor">{{ showColon ? ':' : '分' }}</text>
  9. <text :style="{ borderColor: borderColor, color: color, backgroundColor: backgroundColor }" class="uni-countdown__number">{{ s }}</text>
  10. <text v-if="!showColon" :style="{ color: splitorColor }" class="uni-countdown__splitor">秒</text>
  11. </view>
  12. </template>
  13. <script>
  14. export default {
  15. name: 'UniCountDown',
  16. props: {
  17. showDay: {
  18. type: Boolean,
  19. default: true
  20. },
  21. showColon: {
  22. type: Boolean,
  23. default: true
  24. },
  25. backgroundColor: {
  26. type: String,
  27. default: '#FFFFFF'
  28. },
  29. borderColor: {
  30. type: String,
  31. default: '#000000'
  32. },
  33. color: {
  34. type: String,
  35. default: '#000000'
  36. },
  37. splitorColor: {
  38. type: String,
  39. default: '#000000'
  40. },
  41. day: {
  42. type: Number,
  43. default: 0
  44. },
  45. hour: {
  46. type: Number,
  47. default: 0
  48. },
  49. minute: {
  50. type: Number,
  51. default: 0
  52. },
  53. second: {
  54. type: Number,
  55. default: 0
  56. }
  57. },
  58. data() {
  59. return {
  60. timer: null,
  61. syncFlag: false,
  62. d: '00',
  63. h: '00',
  64. i: '00',
  65. s: '00',
  66. leftTime: 0,
  67. seconds: 0,
  68. startSeconds:0,//初始化时候的时间
  69. }
  70. },
  71. watch: {
  72. day(val) {
  73. this.changeFlag()
  74. },
  75. hour(val) {
  76. this.changeFlag()
  77. },
  78. minute(val) {
  79. this.changeFlag()
  80. },
  81. second(val) {
  82. this.changeFlag()
  83. }
  84. },
  85. created: function(e) {
  86. this.startData();
  87. },
  88. beforeDestroy() {
  89. clearInterval(this.timer);
  90. this.timer = null;
  91. },
  92. methods: {
  93. toSeconds(day, hours, minutes, seconds) {
  94. return day * 60 * 60 * 24 + hours * 60 * 60 + minutes * 60 + seconds
  95. },
  96. //开始计时
  97. timePlay(data){
  98. console.log(data);
  99. if (this.seconds <= 0) {
  100. this.seconds = this.startSeconds;
  101. }
  102. this.timer = setInterval(() => {
  103. this.seconds--
  104. if (this.seconds < 0) {
  105. this.timeUp()
  106. return
  107. }
  108. this.countDown()
  109. }, 1000)
  110. },
  111. //暂停时间
  112. timePause(data){
  113. console.log(data);
  114. clearInterval(this.timer);
  115. this.timer = null;
  116. },
  117. timeUp() {
  118. clearInterval(this.timer);
  119. this.timer = null;
  120. this.$emit('timeup');
  121. },
  122. countDown() {
  123. let seconds = this.seconds
  124. let [day, hour, minute, second] = [0, 0, 0, 0]
  125. if (seconds > 0) {
  126. day = Math.floor(seconds / (60 * 60 * 24))
  127. hour = Math.floor(seconds / (60 * 60)) - (day * 24)
  128. minute = Math.floor(seconds / 60) - (day * 24 * 60) - (hour * 60)
  129. second = Math.floor(seconds) - (day * 24 * 60 * 60) - (hour * 60 * 60) - (minute * 60)
  130. } else {
  131. this.timeUp()
  132. }
  133. if (day < 10) {
  134. day = '0' + day
  135. }
  136. if (hour < 10) {
  137. hour = '0' + hour
  138. }
  139. if (minute < 10) {
  140. minute = '0' + minute
  141. }
  142. if (second < 10) {
  143. second = '0' + second
  144. }
  145. this.d = day
  146. this.h = hour
  147. this.i = minute
  148. this.s = second
  149. },
  150. startData() {
  151. this.startSeconds = this.seconds = this.toSeconds(this.day, this.hour, this.minute, this.second)
  152. if (this.seconds <= 0) {
  153. return
  154. }
  155. this.countDown();
  156. },
  157. changeFlag() {
  158. this.seconds = this.toSeconds(this.day, this.hour, this.minute, this.second)
  159. // this.startData();
  160. this.countDown();
  161. }
  162. }
  163. }
  164. </script>
  165. <style scoped>
  166. @font-face {
  167. font-family: 'UnidreamLED';
  168. src: url('~@/static/font/UnidreamLED.ttf');
  169. }
  170. .uni-countdown {
  171. /* #ifndef APP-NVUE */
  172. display: flex;
  173. /* #endif */
  174. flex-direction: row;
  175. justify-content: flex-start;
  176. padding: 2rpx 0;
  177. }
  178. .uni-countdown__splitor {
  179. /* #ifndef APP-NVUE */
  180. display: flex;
  181. /* #endif */
  182. justify-content: center;
  183. line-height: 48rpx;
  184. padding: 8rpx 0 0 0;
  185. font-size: 24rpx;
  186. }
  187. .uni-countdown__number {
  188. /* #ifndef APP-NVUE */
  189. display: flex;
  190. /* #endif */
  191. justify-content: center;
  192. align-items: center;
  193. width: 56rpx;
  194. height: 56rpx;
  195. line-height: 56rpx;
  196. margin: 5rpx;
  197. text-align: center;
  198. font-size: 23px;
  199. font-family: UnidreamLED;
  200. border-radius: 3px;
  201. }
  202. </style>