当前位置:网站首页>长按按钮执行函数
长按按钮执行函数
2022-07-06 18:02:00 【阿六啊】
在utils文件夹下创建longPress.ts
import {
DirectiveOptions } from 'vue';
const longPress: DirectiveOptions = {
// bind:只调用一次,指令第一次绑定到元素时调用
// el:指令所绑定的元素,可以用来直接操作 DOM。
// binding:一个对象。 name:指令名,value:指令的绑定值
// vnode:编译生成的虚拟节点
bind: function (el, binding, vNode) {
// el就是dom
if (typeof binding.value !== 'function') {
// eslint-disable-next-line
console.log('callback must be a function');
}
// 定义变量
let pressTimer: any = null;
// 运行函数
const handler = (e: Event) => {
binding.value(e);
};
// 创建计时器(1秒后执行函数 )
const start = (e: any) => {
if (e.type === 'click') {
return;
}
if (pressTimer === null) {
pressTimer = setTimeout(() => {
handler(e);
}, 1000);
}
};
// 取消计时器
const cancel = () => {
if (pressTimer !== null) {
clearTimeout(pressTimer);
pressTimer = null;
}
};
// 添加事件监听器
el.addEventListener('mousedown', start);
// 取消计时器
el.addEventListener('click', cancel);
el.addEventListener('mouseout', cancel);
},
// 当传进来的值更新的时候触发
componentUpdated (el: any, {
value }) {
el.$value = value;
},
// 指令与元素解绑的时候,移除事件绑定
unbind (el: any) {
el.removeEventListener('click', el.handler);
}
};
export default longPress;
在需要的文件中引入并且使用
引入
import longpress from '@/utils/longpress';
@Component({
components: {
},
directives: {
longpress
}
})
使用
<basic-button
v-longpress="handleLongMoveLeft"
type="info"
plain
icon="el-icon-arrow-left"
@click="moveLeft"
/>
边栏推荐
- THREE.AxesHelper is not a constructor
- Your cache folder contains root-owned files, due to a bug in npm ERR! previous versions of npm which
- swiper组件中使用video导致全屏错位
- THREE. AxesHelper is not a constructor
- Vocabulary in Data Book
- 【信号与系统】
- Neon Optimization: About Cross access and reverse cross access
- 405 method not allowed appears when the third party jumps to the website
- C# 计算农历日期方法 2022
- LeetCode:1175. 质数排列
猜你喜欢
Make Jar, Not War
身体质量指数程序,入门写死的小程序项目
HMM notes
2022 Google CTF SEGFAULT LABYRINTH wp
JTAG debugging experience of arm bare board debugging
Go zero micro service practical series (IX. ultimate optimization of seckill performance)
AcWing 345. 牛站 题解(floyd的性质、倍增)
Yunna - work order management system and process, work order management specification
LeetCode:1175. 质数排列
Force buckle 1037 Effective boomerang
随机推荐
负载均衡性能参数如何测评?
C语言实例_2
Case development of landlord fighting game
mysqlbackup 还原特定的表
Lldp compatible CDP function configuration
Transformation transformation operator
Neon Optimization: an instruction optimization case of matrix transpose
LeetCode:1175. Prime permutation
C# 计算农历日期方法 2022
Can the system hibernation file be deleted? How to delete the system hibernation file
The cost of returning tables in MySQL
Your cache folder contains root-owned files, due to a bug in npm ERR! previous versions of npm which
一起看看matlab工具箱内部是如何实现BP神经网络的
[case sharing] basic function configuration of network loop detection
让我们,从头到尾,通透网络I/O模型
身体质量指数程序,入门写死的小程序项目
AcWing 346. 走廊泼水节 题解(推公式、最小生成树)
AI automatically generates annotation documents from code
从零开始匹配vim(0)——vimscript 简介
Start from the bottom structure to learn the customization and testing of fpga---- FIFO IP