当前位置:网站首页>长按按钮执行函数
长按按钮执行函数
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"
/>
边栏推荐
- HMM 笔记
- Sword finger offer II 035 Minimum time difference - quick sort plus data conversion
- Taro 小程序开启wxml代码压缩
- 2022 Google CTF segfault Labyrinth WP
- Oracle: Practice of CDB restricting PDB resources
- Make Jar, Not War
- AcWing 1140. 最短网络 (最小生成树)
- 【芯片方案设计】脉搏血氧仪
- C language instance_ four
- Receive user input, height BMI, BMI detection small business entry case
猜你喜欢
域分析工具BloodHound的使用说明
Can the system hibernation file be deleted? How to delete the system hibernation file
HMM 笔记
AcWing 1148. 秘密的牛奶运输 题解(最小生成树)
Make Jar, Not War
AI automatically generates annotation documents from code
【信号与系统】
一起看看matlab工具箱内部是如何实现BP神经网络的
Your cache folder contains root-owned files, due to a bug in npm ERR! previous versions of npm which
The MySQL database in Alibaba cloud was attacked, and finally the data was found
随机推荐
HMM notes
C language instance_ three
Your cache folder contains root-owned files, due to a bug in npm ERR! previous versions of npm which
2022 Google CTF SEGFAULT LABYRINTH wp
AcWing 346. 走廊泼水节 题解(推公式、最小生成树)
WCF基金会
前置机是什么意思?主要作用是什么?与堡垒机有什么区别?
Wood extraction in Halcon
系统休眠文件可以删除吗 系统休眠文件怎么删除
AcWing 345. 牛站 题解(floyd的性质、倍增)
编译命令行终端 swift
LeetCode:1175. 质数排列
Spark TPCDS Data Gen
Body mass index program, entry to write dead applet project
curl 命令
修改px4飞控的系统时间
Dark horse notes - create immutable sets and streams
图片打水印 缩放 和一个输入流的转换
What does front-end processor mean? What is the main function? What is the difference with fortress machine?
安全保护能力是什么意思?等保不同级别保护能力分别是怎样?