uni-count-down.vue 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  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. this.startData();
  106. }
  107. console.log('seconds:',this.seconds,this.startSeconds);
  108. this.timer = setInterval(() => {
  109. this.seconds--
  110. if (this.seconds < 0) {
  111. this.timeUp()
  112. return
  113. }
  114. this.countDown()
  115. }, 1000)
  116. },
  117. //暂停时间
  118. timePause(data){
  119. // console.log(data);
  120. clearInterval(this.timer);
  121. this.timer = null;
  122. },
  123. timeUp() {
  124. clearInterval(this.timer);
  125. this.timer = null;
  126. this.$emit('timeUp');
  127. },
  128. countDown() {
  129. let seconds = this.seconds
  130. let [day, hour, minute, second] = [0, 0, 0, 0]
  131. if (seconds > 0) {
  132. day = Math.floor(seconds / (60 * 60 * 24))
  133. hour = Math.floor(seconds / (60 * 60)) - (day * 24)
  134. minute = Math.floor(seconds / 60) - (day * 24 * 60) - (hour * 60)
  135. second = Math.floor(seconds) - (day * 24 * 60 * 60) - (hour * 60 * 60) - (minute * 60)
  136. } else {
  137. this.timeUp()
  138. }
  139. if (day < 10) {
  140. day = '0' + day
  141. }
  142. if (hour < 10) {
  143. hour = '0' + hour
  144. }
  145. if (minute < 10) {
  146. minute = '0' + minute
  147. }
  148. if (second < 10) {
  149. second = '0' + second
  150. }
  151. this.d = day
  152. this.h = hour
  153. this.i = minute
  154. this.s = second
  155. this.$emit('timeUpdate',{curSeconds:seconds});
  156. },
  157. startData() {
  158. this.startSeconds = this.seconds = this.toSeconds(this.day, this.hour, this.minute, this.second)
  159. if (this.seconds <= 0) {
  160. return
  161. }
  162. this.countDown();
  163. },
  164. changeFlag() {
  165. this.seconds = this.toSeconds(this.day, this.hour, this.minute, this.second);
  166. this.countDown();
  167. }
  168. }
  169. }
  170. </script>
  171. <style scoped>
  172. @font-face {
  173. font-family: 'UnidreamLED';
  174. src: url('~@/static/font/UnidreamLED.ttf');
  175. }
  176. .uni-countdown {
  177. /* #ifndef APP-NVUE */
  178. display: flex;
  179. /* #endif */
  180. flex-direction: row;
  181. justify-content: flex-start;
  182. padding: 2rpx 0;
  183. }
  184. .uni-countdown__splitor {
  185. /* #ifndef APP-NVUE */
  186. display: flex;
  187. /* #endif */
  188. justify-content: center;
  189. line-height: 48rpx;
  190. padding: 8rpx 0 0 0;
  191. font-size: 24rpx;
  192. /* border: 1rpx solid #000000; */
  193. }
  194. .uni-countdown__number {
  195. /* #ifndef APP-NVUE */
  196. display: flex;
  197. /* #endif */
  198. justify-content: center;
  199. align-items: center;
  200. width: 50rpx;
  201. height: 50rpx;
  202. line-height: 56rpx;
  203. margin: 5rpx 0rpx;
  204. text-align: center;
  205. font-size: 23px;
  206. font-family: UnidreamLED;
  207. border-radius: 3px;
  208. }
  209. </style>