uni-count-down.vue 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  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, 'font-size': size+'px' }" 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, 'font-size': size+'px' }" 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. },
  91. beforeDestroy() {
  92. clearInterval(this.timer);
  93. this.timer = null;
  94. },
  95. methods: {
  96. toSeconds(day, hours, minutes, seconds) {
  97. return day * 60 * 60 * 24 + hours * 60 * 60 + minutes * 60 + seconds
  98. },
  99. //开始计时
  100. timePlay(data){
  101. console.log('seconds:',this.seconds,this.startSeconds);
  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. this.$emit('timeUpdate',{curSeconds:seconds});
  150. },
  151. startData() {
  152. this.startSeconds = this.seconds = this.toSeconds(this.day, this.hour, this.minute, this.second)
  153. if (this.seconds <= 0) {
  154. return
  155. }
  156. this.countDown();
  157. },
  158. changeFlag() {
  159. console.log('changeFlag');
  160. this.seconds = this.toSeconds(this.day, this.hour, this.minute, this.second);
  161. this.countDown();
  162. }
  163. }
  164. }
  165. </script>
  166. <style scoped>
  167. @font-face {
  168. font-family: 'UnidreamLED';
  169. src: url('~@/static/font/UnidreamLED.ttf');
  170. }
  171. .uni-countdown {
  172. /* #ifndef APP-NVUE */
  173. display: flex;
  174. /* #endif */
  175. flex-direction: row;
  176. justify-content: flex-start;
  177. padding: 2rpx 0;
  178. }
  179. .uni-countdown__splitor {
  180. /* #ifndef APP-NVUE */
  181. display: flex;
  182. /* #endif */
  183. justify-content: center;
  184. line-height: 48rpx;
  185. /* padding: 8rpx 0 0 0; */
  186. padding: 1px;
  187. font-size: 24rpx;
  188. /* border: 1rpx solid #000000; */
  189. }
  190. .uni-countdown__number {
  191. /* #ifndef APP-NVUE */
  192. display: flex;
  193. /* #endif */
  194. justify-content: center;
  195. align-items: center;
  196. width: 50rpx;
  197. height: 50rpx;
  198. line-height: 56rpx;
  199. margin: 5rpx 0rpx;
  200. text-align: center;
  201. font-size: 23px;
  202. font-family: UnidreamLED;
  203. border-radius: 3px;
  204. }
  205. </style>