com-count-up.php 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  1. <style scoped>
  2. .chartNum {
  3. position: relative;
  4. display: flex;
  5. text-align: center;
  6. justify-content: center;
  7. }
  8. /*滚动数字设置*/
  9. .box-item {
  10. position: relative;
  11. font-size:42px;
  12. line-height: 41px;
  13. text-align: center;
  14. list-style: none;
  15. color: #FFFFFF;
  16. writing-mode: vertical-lr;
  17. text-orientation: upright;
  18. /*文字禁止编辑*/
  19. -moz-user-select: none;
  20. /*火狐*/
  21. -webkit-user-select: none;
  22. /*webkit浏览器*/
  23. -ms-user-select: none;
  24. /*IE10*/
  25. -khtml-user-select: none;
  26. /*早期浏览器*/
  27. user-select: none;
  28. /* overflow: hidden; */
  29. }
  30. /* 默认逗号设置 */
  31. .mark-item {
  32. width: 10px;
  33. margin-right: 5px;
  34. line-height: 10px;
  35. font-size:19px;
  36. position: relative;
  37. }
  38. .mark-item > span {
  39. position: absolute;
  40. width: 100%;
  41. bottom: 0;
  42. writing-mode: vertical-rl;
  43. text-orientation: upright;
  44. }
  45. /*滚动数字设置*/
  46. .number-item {
  47. width: 40px;
  48. list-style: none;
  49. margin-right: 5px;
  50. background: #0A54EA;
  51. }
  52. .number-item > span {
  53. position: relative;
  54. display: inline-block;
  55. height: 100%;
  56. writing-mode: vertical-rl;
  57. text-orientation: upright;
  58. overflow: hidden;
  59. }
  60. .number-item > span > i {
  61. font-style: normal;
  62. position: absolute;
  63. top: 11px;
  64. left: 50%;
  65. transform: translate(-50%, 0);
  66. transition: transform 1s ease-in-out;
  67. letter-spacing: 10px;
  68. }
  69. .number-item:last-child {
  70. margin-right: 0;
  71. }
  72. .end-datetime{
  73. position: absolute;
  74. left: 0;
  75. bottom: -25px;
  76. color: #959595;
  77. font-size: 12px;
  78. }
  79. </style>
  80. <template id="com-count-up">
  81. <div class="com-count-up">
  82. <div class="chartNum">
  83. <div class="box-item">
  84. <!-- <li :class="{'number-item': !isNaN(item) }" v-for="(item,index) in orderNum" :key="index">-->
  85. <!-- <span v-if="!isNaN(item)">-->
  86. <!-- <i ref="numberItem">0123456789</i>-->
  87. <!-- </span>-->
  88. <!-- </li>-->
  89. <div :class="{'number-item': !isNaN(item) }" v-for="(item,index) in orderNum" :key="index" :style="(index+1)%3==0 ? 'margin-right:14px':''">
  90. <span>{{item}}</span>
  91. <div class="mark-item" v-if="(index+1)%3==0 && (index+1)!=9">
  92. <span>,</span>
  93. </div>
  94. </div>
  95. </div>
  96. <div class="end-datetime">{{endDateTime}}</div>
  97. </div>
  98. </div>
  99. </template>
  100. <script>
  101. Vue.component('com-count-up', {
  102. template: '#com-count-up',
  103. props: {
  104. count: '',
  105. end_datetime: ''
  106. },
  107. computed: {
  108. endDateTime(){
  109. return `截止时间${this.end_datetime}`;
  110. }
  111. },
  112. watch: {
  113. data: function(newVal) {
  114. this.toOrderNum(newVal);
  115. },
  116. orderNum: function(newVal) {
  117. // this.setNumberTransform();
  118. }
  119. },
  120. data() {
  121. return {
  122. orderNum: [], // 默认订单总数,
  123. data: '',
  124. };
  125. },
  126. created() {
  127. this.data = JSON.parse(JSON.stringify(this.count));
  128. },
  129. methods: {
  130. updateData() {
  131. setInterval(() => {
  132. this.changeNumber(this.count);
  133. setTimeout(() => {
  134. this.toOrderNum(this.count);
  135. }, 700);
  136. }, 10000);
  137. },
  138. changeNumber(num) {
  139. let half = Math.floor(num * Math.random());
  140. // console.log(num, half);
  141. if (!isNaN(half)) {
  142. this.toOrderNum(half);
  143. }
  144. },
  145. // 处理数字
  146. toOrderNum(num) {
  147. num = num.toString();
  148. if (num.length < 9) {
  149. num = "0" + num; // 如未满9位数,添加"0"补位
  150. this.toOrderNum(num); // 递归添加"0"补位
  151. } else if (num.length === 9) {
  152. this.orderNum = num.split("");
  153. } else {
  154. console.log("超过9位数");
  155. }
  156. },
  157. // 设置文字滚动
  158. setNumberTransform() {
  159. this.$nextTick(() => {
  160. let numberItems = this.$refs.numberItem;
  161. this.numberArr = this.orderNum.filter(item => !isNaN(item));
  162. // 结合CSS 对数字字符进行滚动
  163. for (let index = 0; index < numberItems.length; index++) {
  164. let elem = numberItems[index];
  165. elem.style.transform = `translate(-50%, -${this.numberArr[index] *
  166. 10}%)`;
  167. }
  168. })
  169. }
  170. },
  171. });
  172. </script>