当前位置:网站首页>长按按钮执行函数
长按按钮执行函数
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"
/>
边栏推荐
猜你喜欢

对C语言数组的再认识

Yunna | work order management software, work order management software app

微信公众号发送模板消息

Gazebo的安装&与ROS的连接

AcWing 361. 观光奶牛 题解(spfa求正环)

Transplant DAC chip mcp4725 to nuc980
![[advanced C language] 8 written questions of pointer](/img/d4/c9bb2c8c9fd8f54a36e463e3eb2fe0.png)
[advanced C language] 8 written questions of pointer

go-zero微服务实战系列(九、极致优化秒杀性能)

AcWing 1148. 秘密的牛奶运输 题解(最小生成树)

JTAG principle of arm bare board debugging
随机推荐
负载均衡性能参数如何测评?
HMM notes
前置机是什么意思?主要作用是什么?与堡垒机有什么区别?
JS reverse -- ob confusion and accelerated music that poked the [hornet's nest]
Instructions for using the domain analysis tool bloodhound
Go zero micro service practical series (IX. ultimate optimization of seckill performance)
Start from the bottom structure to learn the customization and testing of fpga---- FIFO IP
安全保护能力是什么意思?等保不同级别保护能力分别是怎样?
golang 基础 —— 数据类型
Body mass index program, entry to write dead applet project
C language instance_ two
Google released a security update to fix 0 days that have been used in chrome
Can the system hibernation file be deleted? How to delete the system hibernation file
Gnet: notes on the use of a lightweight and high-performance go network framework
C language instance_ four
C语言实例_2
LeetCode:1175. 质数排列
Match VIM from zero (0) -- Introduction to vimscript
[JS] obtain the N days before and after the current time or the n months before and after the current time (hour, minute, second, year, month, day)
Spark TPCDS Data Gen