index.vue 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. <template>
  2. <view class="u-demo">
  3. <view class="u-demo-wrap">
  4. <view class="u-demo-title">演示效果</view>
  5. <view class="u-demo-area">
  6. <u-toast ref="uToast"></u-toast>
  7. <view class="u-no-demo-here">如果使用text-align: center对齐,数字滚动期间可能会抖动,见文档说明</view>
  8. <view class="count-to-demo">
  9. <u-count-to
  10. class="count-to"
  11. :useEasing="useEasing"
  12. ref="uCountTo"
  13. :autoplay="autoplay"
  14. :startVal="startVal"
  15. :endVal="endVal"
  16. :duration="duration"
  17. :decimals="decimals"
  18. :bold="bold"
  19. @end="end"
  20. ></u-count-to>
  21. </view>
  22. </view>
  23. </view>
  24. <view class="u-config-wrap">
  25. <view class="u-config-title u-border-bottom">参数配置</view>
  26. <view class="u-config-item">
  27. <view class="u-item-title">状态</view>
  28. <u-subsection :current="current" :list="['启动', '暂停', '继续', '重置']" @change="statusChange"></u-subsection>
  29. </view>
  30. <view class="u-config-item">
  31. <view class="u-item-title">目标值</view>
  32. <u-subsection :list="[608, 5604, 45617]" @change="endValChange"></u-subsection>
  33. </view>
  34. <view class="u-config-item">
  35. <view class="u-item-title">滚动时间</view>
  36. <u-subsection current="1" :list="[1000, 2000, 3000]" @change="durationChange"></u-subsection>
  37. </view>
  38. <view class="u-config-item">
  39. <view class="u-item-title">显示小数</view>
  40. <u-subsection current="1" :list="['是', '否']" @change="decimalsChange"></u-subsection>
  41. </view>
  42. <view class="u-config-item">
  43. <view class="u-item-title">字体加粗</view>
  44. <u-subsection current="1" :list="['是', '否']" @change="boldChange"></u-subsection>
  45. </view>
  46. </view>
  47. </view>
  48. </template>
  49. <script>
  50. export default {
  51. data() {
  52. return {
  53. startVal: 0,
  54. endVal: 608,
  55. separator: ',',
  56. decimals: 0,
  57. duration: 2000,
  58. autoplay: false,
  59. useEasing: true,
  60. current: 3,
  61. isStop: false, // 如果开没启动前,不允许点击状态选项的"继续"按钮,否则会导致显示NaN
  62. bold: false
  63. };
  64. },
  65. methods: {
  66. endValChange(index) {
  67. this.endVal = index == 0 ? 608 : index == 1 ? 5604 : 45617;
  68. this.reset();
  69. this.start();
  70. },
  71. durationChange(index) {
  72. this.duration = index == 0 ? 1000 : index == 1 ? 2000 : 3000;
  73. },
  74. boldChange(index) {
  75. this.bold = !!!index;
  76. },
  77. decimalsChange(index) {
  78. this.decimals = index == 0 ? 2 : 0;
  79. },
  80. statusChange(index) {
  81. this.current = index;
  82. if (index == 0) {
  83. this.start();
  84. } else if (index == 1) {
  85. this.stop();
  86. } else if (index == 2) {
  87. this.resume();
  88. } else {
  89. this.reset();
  90. }
  91. },
  92. end() {
  93. this.current = 3;
  94. this.$refs.uToast.show({
  95. type: 'warning',
  96. title: '滚动结束'
  97. });
  98. },
  99. start() {
  100. this.current = 0;
  101. this.isStop = true;
  102. this.$refs.uCountTo.start();
  103. },
  104. stop() {
  105. this.$refs.uCountTo.stop();
  106. },
  107. resume() {
  108. if (!this.isStop) {
  109. this.$refs.uToast.show({
  110. type: 'error',
  111. title: '请开始并暂停后才能继续'
  112. });
  113. this.$nextTick(() => {
  114. this.current = 3;
  115. });
  116. return;
  117. }
  118. this.$refs.uCountTo.resume();
  119. },
  120. reset() {
  121. this.$refs.uCountTo.reset();
  122. }
  123. }
  124. };
  125. </script>
  126. <style lang="scss" scoped>
  127. .count-to-demo {
  128. text-align: center;
  129. }
  130. </style>