vue自定义指令长按事件
2024-09-05 04:05:15
Vue.directive('longpress', {
bind: function (el, binding, vNode) {
// Make sure expression provided is a function
if (typeof binding.value !== 'function') {
// Fetch name of component
const compName = vNode.context.name
// pass warning to console
let warn = `[longpress:] provided expression '${binding.expression}' is not a function, but has to be`
if (compName) { warn += `Found in component '${compName}' ` }
console.warn(warn)
}
// Define variable
let pressTimer = null
// Define funtion handlers
// Create timeout ( run function after 1s )
let start = (e) => {
if (e.type === 'click' && e.button !== 0) {
return;
}
if (pressTimer === null) {
pressTimer = setTimeout(() => {
// Run function
handler()
}, 1000)
}
}
// Cancel Timeout
let cancel = (e) => {
// Check if timer has a value or not
if (pressTimer !== null) {
clearTimeout(pressTimer)
pressTimer = null
}
}
// Run Function
const handler = (e) => {
binding.value(e)
}
// Add Event listeners
el.addEventListener("mousedown", start);
el.addEventListener("touchstart", start);
// Cancel timeouts if this events happen
el.addEventListener("click", cancel);
el.addEventListener("mouseout", cancel);
el.addEventListener("touchend", cancel);
el.addEventListener("touchcancel", cancel);
}
})
最新文章
- Java Collection好文章
- 多个UIImage合并成一个UIImage
- JS实现自动倒计时
- Oracle创建表
- android studio只能全部提示设置
- [cocos2d-x3.0]Android+NDK+Eclipse环境搭建及编译步骤~
- javascript基础学习(六)
- 在CentOS 7下ISCSI和多路径部署文档【转】
- [js高手之路]gulp教程-从入门到项目中快速上手使用
- HTTP协议篇(一):多工、数据流
- JavaScript 随机数相关算法
- Xamarin.Android 使用AsyncTask提示上传动态
- nis+kerberos 实现服务验证
- C#实现全局快捷键(系统热键)响应(转)
- redis5.0新特性
- MySQL免安装版,遇到MSVCR120.dll文件丢失错误的解决方案
- TensorFlow 常用的函数
- Android界面设计
- js 获取读取cookie
- 小米范工具系列之四:小米范HTTP批量发包器
热门文章
- vue学习04 v-on指令
- burp suite 之 Decoder(解码) Comparer(比较器) Extender(扩展)
- ucore操作系统学习笔记(一) ucore lab1系统启动流程分析
- ViewBinding的简单使用
- Spring与Junit测试整合
- C++重载>;>;和<;<;(输入和输出运算符)详解
- 【题解】CF940F Machine Learning
- 安装 Windows 10 系统时分区选择 MBR 还是 GUID?
- Windows10系统下wsappx占用CPU资源过高?wsappx是什么?如何关闭wsappx进程?
- 第3天 | 12天搞定Python,用PyCharm编写代码