index.vue 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  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. <view class="no-mode-here">
  7. 选择节流或者防抖模式,点击按钮,将会执行回调并显示在下方:
  8. </view>
  9. <view class="u-demo-result-line" v-if="result.length">
  10. <view v-for="(item, index) in result" :key="index">{{(index >= 1) ? '-' : ''}}回调</view>
  11. </view>
  12. </view>
  13. </view>
  14. <view class="u-config-wrap">
  15. <view class="u-button" hover-class="u-button--hover" hover-stay-time="150" @tap="btnClick">
  16. 点击触发
  17. </view>
  18. <view class="u-config-title u-border-bottom">
  19. 参数配置
  20. </view>
  21. <view class="u-config-item">
  22. <view class="u-item-title">模式</view>
  23. <u-subsection :list="['节流', '防抖']" @change="modeChange"></u-subsection>
  24. </view>
  25. <view class="u-config-item">
  26. <view class="u-item-title">时间间隔</view>
  27. <u-subsection current="1" :list="['500ms', '1000ms', '2000ms']" @change="timeoutChange"></u-subsection>
  28. </view>
  29. <view class="u-config-item">
  30. <view class="u-item-title">执行时机</view>
  31. <u-subsection :list="['开始处', '结束处']" @change="immediateChange"></u-subsection>
  32. </view>
  33. </view>
  34. </view>
  35. </template>
  36. <script>
  37. export default {
  38. data() {
  39. return {
  40. result: [],
  41. timeout: 1000,
  42. immediate: true,
  43. mode: 'throttle'
  44. }
  45. },
  46. methods: {
  47. modeChange(index) {
  48. this.mode = index ? 'debouncd' : 'throttle';
  49. },
  50. timeoutChange(index) {
  51. this.timeout = [500, 1000, 2000][index];
  52. },
  53. immediateChange(index) {
  54. this.immediate = !index;
  55. },
  56. getResult() {
  57. if(this.result.length >= 6) this.result = [];
  58. this.result.push(0);
  59. },
  60. btnClick() {
  61. if(this.mode == 'throttle') {
  62. this.$u.throttle(this.getResult, this.timeout, this.immediate);
  63. } else {
  64. this.$u.debounce(this.getResult, this.timeout, this.immediate);
  65. }
  66. }
  67. }
  68. }
  69. </script>
  70. <style lang="scss" scoped>
  71. .u-button {
  72. margin-top: 50rpx;
  73. margin-bottom: 50rpx;
  74. display: flex;
  75. justify-content: center;
  76. align-items: center;
  77. height: 80rpx;
  78. border-radius: 6rpx;
  79. border: 1px solid $u-type-primary;
  80. color: $u-type-primary;
  81. }
  82. .u-button--hover {
  83. color: #fff;
  84. background-color: $u-type-primary;
  85. }
  86. .u-demo-result-line {
  87. display: flex;
  88. justify-content: center;
  89. }
  90. </style>