当前位置:网站首页>长按按钮执行函数
长按按钮执行函数
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"
/>
边栏推荐
- Your cache folder contains root-owned files, due to a bug in npm ERR! previous versions of npm which
- [case sharing] basic function configuration of network loop detection
- AcWing 345. 牛站 题解(floyd的性质、倍增)
- Share a general compilation method of so dynamic library
- LeetCode:1175. 质数排列
- table表格设置圆角
- Table table setting fillet
- ZOJ Problem Set – 2563 Long Dominoes 【如压力dp】
- Instructions for using the domain analysis tool bloodhound
- THREE.AxesHelper is not a constructor
猜你喜欢

Go zero micro service practical series (IX. ultimate optimization of seckill performance)

Transformation transformation operator

Gazebo的安装&与ROS的连接

Boot - Prometheus push gateway use

修改px4飞控的系统时间

AcWing 345. 牛站 题解(floyd的性质、倍增)

对C语言数组的再认识

LeetCode:1175. Prime permutation

2022 Google CTF segfault Labyrinth WP

云呐|工单管理软件,工单管理软件APP
随机推荐
JTAG principle of arm bare board debugging
LeetCode:1175. 质数排列
Neon Optimization: performance optimization FAQ QA
公钥\私人 ssh避password登陆
Instructions for using the domain analysis tool bloodhound
Send template message via wechat official account
接收用户输入,身高BMI体重指数检测小业务入门案例
Google released a security update to fix 0 days that have been used in chrome
Js逆向——捅了【马蜂窝】的ob混淆与加速乐
[signal and system]
LeetCode. 剑指offer 62. 圆圈中最后剩下的数
Body mass index program, entry to write dead applet project
Byte P7 professional level explanation: common tools and test methods for interface testing, Freeman
How to manage distributed teams?
C language instance_ five
C language instance_ two
移植DAC芯片MCP4725驱动到NUC980
云呐|工单管理软件,工单管理软件APP
Taro2.* 小程序配置分享微信朋友圈
uva 1401 dp+Trie