首页 > 其他分享 >vue长按事件指令(v-longPress)

vue长按事件指令(v-longPress)

时间:2023-04-13 14:59:22浏览次数:36  
标签:el vue longPress 指令 addEventListener cancel null pressTimer

import type { Directive, App } from 'vue';

const longPress: Directive = {
  beforeMount: function (el, binding, vnode, prevVnode) {
    if (typeof binding.value !== 'function') {
      throw 'callback must be a function';
    }
    if (el) {
      el.className = el.className + ' no_select';
    }

    // 定义变量
    let pressTimer: number | null = null;
    // 创建计时器( 1秒后执行函数 )
    let start = (e: Event) => {
      if (e.type === 'click') {
        return;
      }
      if (pressTimer === null) {
        pressTimer = window.setTimeout(() => {
          handler(e);
        }, 1000);
      }
    };
    // 取消计时器
    let cancel = () => {
      if (pressTimer !== null) {
        clearTimeout(pressTimer);
        pressTimer = null;
      }
    };
    // 运行函数
    const handler = (e: any) => {
      binding.value(e);
    };
    // 添加事件监听器
    el.addEventListener('mousedown', start);
    el.addEventListener('touchstart', start);
    // 取消计时器
    el.addEventListener('click', cancel);
    el.addEventListener('mouseout', cancel);
    el.addEventListener('touchend', cancel);
    el.addEventListener('touchcancel', cancel);
    el.addEventListener('mouseup', cancel);
  },
};

export function setupLongPressDirective(app: App) {
  app.directive('longPress', longPress);
}

export default longPress;

标签:el,vue,longPress,指令,addEventListener,cancel,null,pressTimer
From: https://www.cnblogs.com/songkomei/p/17314788.html

相关文章

  • VUE.JS和NODE.JS构建一个简易的前后端分离静态博客系统(三)
    Edit.vue<template><divid="edit"><ClassicHeader><templatev-slot:left><span>编辑随笔</span></template><templatev-slot:right><el-button@click="......
  • VUE.JS和NODE.JS构建一个简易的前后端分离静态博客系统(二)
    后台管理页面,需要配合NODE.JS搭建的EXPRESS服务器使用。main.jsimportVuefrom'vue'importAppfrom'./App.vue'importrouterfrom'./router'import{Button,Input,Form,Link,Divider,Upload,Dialog,Card,Popover,Messa......
  • Vue.js 独享路由守卫
    视频独享守卫:beforeEnter(to,from,next){ console.log('beforeEnter',to,from) if(to.meta.isAuth){//判断当前路由是否需要进行权限控制 if(localStorage.getItem('school')==='atguigu'){ next() }else{ alert('暂无权限查看') //next(......
  • Vuex module之间调用与读值
    对于模块内部的mutation和getter,接收的第一个参数是模块的局部状态对象state对于模块内部的action,局部状态通过context.state暴露出来,根节点状态则为context.rootState使用全局state和getter,rootState和rootGetters会作为第三和第四参数传入getter,也会通过conte......
  • 基于Java+Springboot+vue网上商品订单转手系统设计和实现
    基于Java+Springboot+vue网上商品订单转手系统设计和实现一、前言介绍:1.1项目摘要传统办法管理信息首先需要花费的时间比较多,其次数据出错率比较高,而且对错误的数据进行更改也比较困难,最后,检索数据费事费力。因此,在计算机上安装网上商品订单转手系统软件来发挥其高效地信息处理......
  • vue中父子组件调用之头像更新
    问题描述:修改图像的功能,要实现的功能是这样的:点击确认按钮,然后连带着上面的头像也更新,是不是很简单,只需要一个刷新就行,但是事实上没这么简单,因为我的这个项目涉及到三个组件 这里面中间是一个子路由,然后上面是一个组件,左边是一个组件  然后这个就涉及到子父组件传值,这......
  • 计算机组成原理-指令集体系结构(一)
    计算机的指令集体系结构(ISA)从汇编语言程序员的角度描述了计算机,并强调了计算机的功能,而不是它的内部组成实现。ISA说明了计算机能做什么,而计算机组成则说明了它是如何做的。学习目标:1.分析存储程序计算机并演示指令如何执行。2.了解存储器-存储器、寄存器-存......
  • vue路由懒加载
    vue路由懒加载是性能优化考虑的一种策略。假如在router内需要引入一个component文件,importPrevCompfrom'./components/prev-comp'importNextCompfrom'./components/next-comp'这是常规的文件引入方式,这种方式下有用到和没有用到的文件都会被引入,增加资源加载性能消耗......
  • Vue.js 两个新的生命周期钩子(路由组件独有)
    视频11.两个新的生命周期钩子作用:路由组件所独有的两个钩子,用于捕获路由组件的激活状态。具体名字:activated路由组件被激活时触发。deactivated路由组件失活时触发。>Home.vueNews.vuecomponentsBanner.vue<template> <divclass="col-xs-offset-2col-xs-8"> ......
  • Vue.js 编程式路由导航
    视频>router-link最后会转成标签,有时候页面不是a标签就不能用router-link来写9.编程式路由导航作用:不借助<router-link>实现路由跳转,让路由跳转更加灵活具体编码://$router的两个APIthis.$router.push({ name:'xiangqing', params:{ id:xxx, title:xxx ......