blog

実用的なVueカスタムブレ防止ディレクティブ

アンチシェイク機能、デフォルト200ms 使用方法 throttle function 使用方法 直接v-debounceまたはv-throttleでトリガーする機能をバインドします。...

Sep 4, 2020 · 2 min. read
シェア

アンチシェイク機能、デフォルト200ms

/** * * @date * @description clickアンチバウンス、デフォルト200ms */ export default { bind: (el, binding) => { if(typeof binding.value !== 'function'){ throw new Error('v-debounce not a function') } let timer = null el.addEventListener('click', () => { if (timer) clearTimeout(timer) timer = setTimeout(() => { binding.value() timer = null }, 200) }) } }

使用方法

<el-button v-debounce="foo"> </el-button> import debounce from 'directive/debounce' export default { directives: { debounce } methods: { foo() { console.log('クリックアンチスロットル機能') } }}

スロットル機能

/** * * @date * @description clickスロットル、デフォルト500ms */ export default { bind: (el, binding) => { if(typeof binding.value !== 'function'){ throw new Error('v-throttle not a function') } let timer = null el.addEventListener('click', () => { if (!timer) binding.value() timer = setTimeout(() => { clearTimeout(timer) timer = null }, 500) }) }}

使用方法

<el-button v-throttle="foo"> </el-button> import throttle from 'directive/throttle' export default { directives: { throttle }, methods: { foo() { console.log('スロットル機能がクリックされる') } } }

直接v-デバウンスまたはv-スロットルは、@クリックが再びトリガされることなくトリガされる機能をバインドします。

Read next

vscodeの問題点

vscode unknow word cspell adjust level can't connect 解決策は以下のとおりです。

Sep 4, 2020 · 1 min read