百变鹏仔-专注前端行业精选
vue按钮限制连点封装 自定义指令限制连点
作者:鹏仔先生日期:2023-10-08 17:21:47浏览:626分类:JavaScript
最近测试提出连点bug,但是记得之前都加了连点限制,想了想才想起部分按钮用的a标签或者其他标签实现的,之前限制连点用的 display:none; 属性,在针对button按钮时是可以限制连点的,但是对于span、a等标签是无法限制连点,所以就用到了CSS中的 pointer-events: none; 属性,这个属性在鹏仔之前写的网页水印插件中也有使用 https://www.sharedbk.com/post/200.html 属性具体大家可百度了解。
好了,下面是对应代码以及添加教程
首先我们新建一个js文件,例如 plugins.js
import Vue from 'vue'; const pointDoms = []; // 使用这个指令的所有DOM对象 Vue.directive('points', { inserted(el, binding) { pointDoms.push(el); // 存储使用这个指令的DOM el.addEventListener('click', () => { // 禁用所有使用这个指令的DOM结构点击事件 pointDoms.forEach(pointItem => { pointItem.style.pointerEvents = 'none'; }); setTimeout(() => { // 启动所有使用这个指令的DOM结构点击事件 pointDoms.forEach(pointItem => { pointItem.style.pointerEvents = 'auto'; }); }, binding.value || 1500); }); } });
在main.js中自行引入(自行修改自己的路径)
import "@/assets/resource/plugins.js"; // 引入自定义指令防止连点
使用时只需要在需要点击的按钮上加上自定义的指令“v-points”,例如
<span v-points="1000">百变鹏仔</span> <button v-points>百变鹏仔</button>
默认为1500则为1.5秒,可自定义连点限制时间。
手机扫码访问
猜你还喜欢
- 04-19 VUE实现点击复制
- 04-16 vue将页面生成图片 vue生成海报
- 04-16 vue路由切换滑动效果 vue页面跳转交互 vue实现动画跳转
- 04-16 table固定表头和列 css实现表格固定表头
- 04-07 vue跳转页面清除历史记录,页面跳转删除历史记录
- 02-22 VUE You are using the runtime-only build of Vue where the template compiler is not available. Either
- 01-19 elementui多选上传 before-upload 格式效验错误总会触发before-remove (elementui多选上传on-success只执行了一次,只上传成功了一条)
- 09-14 nvm常用命令有哪些?nvm如何切换node版本?nvm如何下载node?
- 08-19 使用HBuilderX将vue或H5项目打包app
- 07-15 小程序嵌入网页向小程序跳转并传参,微信小程序中实现公众号授权获取openId
- 07-13 vue中实现文件批量打包压缩下载(以及下载跨域问题分析)
- 07-08 uniapp调用地图,进行位置查询,标记定位
暂无评论,来添加一个吧。