numberScroll.vue 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. <template>
  2. <view class="number-box">
  3. <block v-for="(myIndex, index) in indexArr" :key="index">
  4. <swiper class="swiper" vertical="true" :current="myIndex" circular="true"
  5. v-bind:style="{color:color,width:myIndex == 10 ? '14rpx' : myIndex == 1 ? '22rpx' : width+'rpx',height:height+'rpx',lineHeight:fontSize+'rpx',fontSize:fontSize+'rpx',fontWeight: fontWeight}">
  6. <swiper-item>
  7. <view class="swiper-item">0</view>
  8. </swiper-item>
  9. <swiper-item>
  10. <view class="swiper-item">1</view>
  11. </swiper-item>
  12. <swiper-item>
  13. <view class="swiper-item">2</view>
  14. </swiper-item>
  15. <swiper-item>
  16. <view class="swiper-item">3</view>
  17. </swiper-item>
  18. <swiper-item>
  19. <view class="swiper-item">4</view>
  20. </swiper-item>
  21. <swiper-item>
  22. <view class="swiper-item">5</view>
  23. </swiper-item>
  24. <swiper-item>
  25. <view class="swiper-item">6</view>
  26. </swiper-item>
  27. <swiper-item>
  28. <view class="swiper-item">7</view>
  29. </swiper-item>
  30. <swiper-item>
  31. <view class="swiper-item">8</view>
  32. </swiper-item>
  33. <swiper-item>
  34. <view class="swiper-item">9</view>
  35. </swiper-item>
  36. <swiper-item>
  37. <view class="swiper-item">.</view>
  38. </swiper-item>
  39. </swiper>
  40. </block>
  41. </view>
  42. </template>
  43. <script>
  44. export default {
  45. props: {
  46. num: [String, Number],
  47. color: {
  48. type: String,
  49. default: '#000000'
  50. },
  51. width: {
  52. type: String,
  53. default: '30'
  54. },
  55. height: {
  56. type: String,
  57. default: '30'
  58. },
  59. fontSize: {
  60. type: String,
  61. default: '30'
  62. },
  63. fontWeight: {
  64. type: [String, Number],
  65. default: 500
  66. }
  67. },
  68. data() {
  69. return {
  70. indexArr: []
  71. };
  72. },
  73. created() {
  74. let {
  75. num
  76. } = this;
  77. let arr = new Array(num.toString().length);
  78. arr.fill(0);
  79. this.indexArr = arr;
  80. },
  81. watch: {
  82. num: function(val, oldVal) {
  83. // 处理新老数据长度不一样的情况
  84. let arr = Array.prototype.slice.apply(this.indexArr);
  85. let newLen = val.toString().length;
  86. let oldLen = oldVal.toString().length;
  87. if (newLen > oldLen) {
  88. for (let i = 0; i < newLen - oldLen; i++) {
  89. arr.push(0);
  90. }
  91. this.indexArr = arr;
  92. }
  93. if (newLen < oldLen) {
  94. for (let i = 0; i < oldLen - newLen; i++) {
  95. arr.pop();
  96. }
  97. this.indexArr = arr;
  98. }
  99. this.numChange(val);
  100. }
  101. },
  102. mounted() {
  103. //定时器作用:app显示数字滚动
  104. this._time = setTimeout(() => {
  105. this.numChange(this.num);
  106. clearTimeout(this._time);
  107. }, 50);
  108. },
  109. methods: {
  110. /**
  111. * 数字改变
  112. * @value 数字
  113. */
  114. numChange(num) {
  115. this.$nextTick(() => {
  116. let {
  117. indexArr
  118. } = this;
  119. let copyIndexArr = Array.prototype.slice.apply(indexArr);
  120. let _num = num.toString();
  121. for (let i = 0; i < _num.length; i++) {
  122. if (_num[i] === '.') {
  123. copyIndexArr[i] = 10;
  124. } else {
  125. copyIndexArr[i] = Number(_num[i]);
  126. }
  127. }
  128. this.indexArr = copyIndexArr;
  129. })
  130. }
  131. }
  132. };
  133. </script>
  134. <style lang="scss">
  135. .number-box {
  136. display: flex;
  137. flex-wrap: wrap;
  138. justify-content: center;
  139. }
  140. .swiper {
  141. position: relative;
  142. // line-height: 30upx;
  143. // width: 30upx;
  144. // height: 30upx;
  145. // font-size: 30upx;
  146. // background: red;
  147. }
  148. .swiper:after {
  149. content: '';
  150. position: absolute;
  151. left: 0;
  152. top: 0;
  153. width: 100%;
  154. height: 100%;
  155. }
  156. </style>