|
| 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 vibrateShort :list="['节流', '防抖']" @change="modeChange"></u-subsection> |
| 24 | + </view> |
| 25 | + <view class="u-config-item"> |
| 26 | + <view class="u-item-title">时间间隔</view> |
| 27 | + <u-subsection vibrateShort 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 vibrateShort :list="['开始处', '结束处']" @change="immediateChange"></u-subsection> |
| 32 | + </view> |
| 33 | + </view> |
| 34 | + </view> |
| 35 | +</template> |
| 36 | + |
| 37 | +<script> |
| 38 | + export default { |
| 39 | + data() { |
| 40 | + return { |
| 41 | + result: [], |
| 42 | + timeout: 1000, |
| 43 | + immediate: true, |
| 44 | + mode: 'throttle' |
| 45 | + } |
| 46 | + }, |
| 47 | + methods: { |
| 48 | + modeChange(index) { |
| 49 | + this.mode = index ? 'debouncd' : 'throttle'; |
| 50 | + }, |
| 51 | + timeoutChange(index) { |
| 52 | + this.timeout = [500, 1000, 2000][index]; |
| 53 | + }, |
| 54 | + immediateChange(index) { |
| 55 | + this.immediate = !index; |
| 56 | + }, |
| 57 | + getResult() { |
| 58 | + if(this.result.length >= 6) this.result = []; |
| 59 | + this.result.push(0); |
| 60 | + }, |
| 61 | + btnClick() { |
| 62 | + if(this.mode == 'throttle') { |
| 63 | + this.$u.throttle(this.getResult, this.timeout, this.immediate); |
| 64 | + } else { |
| 65 | + this.$u.debounce(this.getResult, this.timeout, this.immediate); |
| 66 | + } |
| 67 | + } |
| 68 | + } |
| 69 | + } |
| 70 | +</script> |
| 71 | + |
| 72 | +<style lang="scss" scoped> |
| 73 | + .u-button { |
| 74 | + margin-top: 50rpx; |
| 75 | + margin-bottom: 50rpx; |
| 76 | + display: flex; |
| 77 | + justify-content: center; |
| 78 | + align-items: center; |
| 79 | + height: 80rpx; |
| 80 | + border-radius: 6rpx; |
| 81 | + border: 1px solid $u-type-primary; |
| 82 | + color: $u-type-primary; |
| 83 | + } |
| 84 | + |
| 85 | + .u-button--hover { |
| 86 | + color: #fff; |
| 87 | + background-color: $u-type-primary; |
| 88 | + } |
| 89 | + |
| 90 | + .u-demo-result-line { |
| 91 | + display: flex; |
| 92 | + justify-content: center; |
| 93 | + } |
| 94 | +</style> |
0 commit comments