uni-count-down.vue 4.8 KB

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