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);
    }
})

最新文章

  1. Java Collection好文章
  2. 多个UIImage合并成一个UIImage
  3. JS实现自动倒计时
  4. Oracle创建表
  5. android studio只能全部提示设置
  6. [cocos2d-x3.0]Android+NDK+Eclipse环境搭建及编译步骤~
  7. javascript基础学习(六)
  8. 在CentOS 7下ISCSI和多路径部署文档【转】
  9. [js高手之路]gulp教程-从入门到项目中快速上手使用
  10. HTTP协议篇(一):多工、数据流
  11. JavaScript 随机数相关算法
  12. Xamarin.Android 使用AsyncTask提示上传动态
  13. nis+kerberos 实现服务验证
  14. C#实现全局快捷键(系统热键)响应(转)
  15. redis5.0新特性
  16. MySQL免安装版,遇到MSVCR120.dll文件丢失错误的解决方案
  17. TensorFlow 常用的函数
  18. Android界面设计
  19. js 获取读取cookie
  20. 小米范工具系列之四:小米范HTTP批量发包器

热门文章

  1. vue学习04 v-on指令
  2. burp suite 之 Decoder(解码) Comparer(比较器) Extender(扩展)
  3. ucore操作系统学习笔记(一) ucore lab1系统启动流程分析
  4. ViewBinding的简单使用
  5. Spring与Junit测试整合
  6. C++重载>>和<<(输入和输出运算符)详解
  7. 【题解】CF940F Machine Learning
  8. 安装 Windows 10 系统时分区选择 MBR 还是 GUID?
  9. Windows10系统下wsappx占用CPU资源过高?wsappx是什么?如何关闭wsappx进程?
  10. 第3天 | 12天搞定Python,用PyCharm编写代码