当前位置:网站首页>长按按钮执行函数
长按按钮执行函数
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"
/>
边栏推荐
- Clickhouse fields are grouped and aggregated, and SQL is queried according to the granularity of any time period
- How to prevent overfitting in cross validation
- [signal and system]
- Case development of landlord fighting game
- Neon Optimization: an optimization case of log10 function
- swiper组件中使用video导致全屏错位
- Neon Optimization: summary of performance optimization experience
- 身体质量指数程序,入门写死的小程序项目
- Your cache folder contains root-owned files, due to a bug in npm ERR! previous versions of npm which
- Taro 小程序开启wxml代码压缩
猜你喜欢
go-zero微服务实战系列(九、极致优化秒杀性能)
Start from the bottom structure to learn the customization and testing of fpga---- FIFO IP
系统休眠文件可以删除吗 系统休眠文件怎么删除
对C语言数组的再认识
c语言—数组
2022 Google CTF SEGFAULT LABYRINTH wp
修改px4飞控的系统时间
云呐-工单管理制度及流程,工单管理规范
Force buckle 1037 Effective boomerang
Body mass index program, entry to write dead applet project
随机推荐
黑马笔记---异常处理
Sword finger offer II 035 Minimum time difference - quick sort plus data conversion
hdu 4661 Message Passing(木DP&amp;组合数学)
Asset security issues or constraints on the development of the encryption industry, risk control + compliance has become the key to breaking the platform
What are the differences between Oracle Linux and CentOS?
go-zero微服务实战系列(九、极致优化秒杀性能)
Byte P7 professional level explanation: common tools and test methods for interface testing, Freeman
域分析工具BloodHound的使用说明
C language instance_ five
Make Jar, Not War
The MySQL database in Alibaba cloud was attacked, and finally the data was found
Vocabulary in Data Book
从底层结构开始学习FPGA----FIFO IP的定制与测试
Supersocket 1.6 creates a simple socket server with message length in the header
454-百度面经1
736. LISP syntax parsing: DFS simulation questions
Clickhouse fields are grouped and aggregated, and SQL is queried according to the granularity of any time period
今日问题-2022/7/4 lambda体中修改String引用类型变量
【芯片方案设计】脉搏血氧仪
身体质量指数程序,入门写死的小程序项目