实现一个自定义event事件,包括on ,off,trigger,once
on监听事件,off取消事件 ,trigger触发事件,once只执行一次
class Event {
constructor() {
this.handlers = {};//记录所有的事件以及处理函数
/*
{
click:[fn1,fn2],
mouseover: [fn3,fn4]
}
*/
}
/**
* on 添加事件监听
* @param {事件类型} type
* @param {事件处理函数} handler
*/
on(type, handler, once = false) {
if (!this.handlers[type]) {
this.handlers[type] = [];
}
if (!this.handlers[type].includes(handler)) {
this.handlers[type].push(handler);
handler.once = once;
}
}
/**
* off 取消事件监听
* @param {要取消的事件类型} type
* @param {要取消的事件函数,如果不传则清除所有} handler
*/
off(type, handler) {
if (this.handlers[type]) {
if (handler === undefined) {
this.handlers[type] = [];
} else {
this.handlers[type] = this.handlers[type].filter(f => f != handler);
}
}
}
/**
* trigger 执行函数
* @param {要执行哪个类型的函数} type
* @param {事件对象} eventData
* @param {this执行} point
*/
trigger(type, eventData = {}, point = this) {
if (this.handlers[type]) {
this.handlers[type].forEach(f => {
f.call(point, eventData);
if (f.once) {
this.off(type, f);
}
});
}
}
/**
* once 只执行一次
* @param {事件类型} type
* @param {要执行的函数} handler
*/
once(type, handler) {
this.on(type, handler, true);
}
}
测试脚本
let e = new Event;
e.on("click", () => {
console.log(1); })
e.on("click", () => {
console.log(2); })
function fn3() {
console.log(3); }
e.on("click", fn3);
console.log(e);
或者可以使用上一章节的内容进行测试https://www.cnblogs.com/yinping/p/10697083.html
在function函数添加监听函数
(function () {
let box = document.querySelector("#box");
let dragbox = new Drag(box);
dragbox.on("dragstart", function (e) { //这里不可以使用箭头函数,否则trigger的call方法无效
console.log(e, this);
console.log("开始拖拽");
this.style.background = "yellow";
})
dragbox.on("dragend", function (e) {
console.log(e, this);
this.style.background = "red";
})
dragbox.once("drag", function () {
console.log("drag");
})
})()
同时在move,start中添加触发函数
class Drag extends Event {
//构造函数
constructor(el) {
super();
this.el = el;
//鼠标摁下时的元素位置
this.startOffset = {};
//鼠标摁下时的鼠标位置
this.startPoint = {};
let move = (e) => {
this.move(e);
};
let end = (e) => {
document.removeEventListener("mousemove", move);
document.removeEventListener("mouseup", end);
this.trigger("dragend", e, this.el);
};
el.addEventListener("mousedown", (e) => {
this.start(e);
document.addEventListener("mousemove", move);
document.addEventListener("mouseup", end);
})
}
//摁下时的处理函数
start(e) {
let { el } = this;
this.startOffset = {
x: el.offsetLeft,
y: el.offsetTop
}
this.startPoint = {
x: e.clientX,
y: e.clientY
}
this.trigger("dragstart", e, el);
}
//鼠标移动时的处理函数
move(e) {
let { el, startOffset, startPoint } = this;
let newPoint = {
x: e.clientX,
y: e.clientY
}
let dis = {
x: newPoint.x - startPoint.x,
y: newPoint.y - startPoint.y,
}
el.style.left = dis.x + startOffset.x + "px";
el.style.top = dis.y + startOffset.y + "px";
this.trigger('drag', e, el);
}
}
实现一个自定义event事件,包括on ,off,trigger,once的更多相关文章
- CAD由一个自定义实体事件中的id得到自定义实体对象(com接口VB语言)
由一个自定义实体事件中的id得到自定义实体对象.该函数只能在自定义实体事件中调用. 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 2 ...
- spring中自定义Event事件的使用和浅析
在我目前接触的项目中,用到了许多spring相关的技术,框架层面的spring.spring mvc就不说了,细节上的功能也用了不少,如schedule定时任务.Filter过滤器. intercep ...
- Spring学习六:自定义Event事件
Spring 中的自定义事件 编写和发布自己的自定义事件有许多步骤.按照在这一章给出的说明来编写,发布和处理自定义 Spring 事件. 步骤 描述 1 创建一个名称为 SpringExample 的 ...
- WPF路由事件三:自定义路由事件
与依赖项属性类似,WPF也为路由事件提供了WPF事件系统这一组成.为一个类型添加一个路由事件的方式与为类型添加依赖项属性的方法类似,添加一个自定义路由事件的步骤: 一.声明路由事件变量并注册:定义只读 ...
- JQuery常用API 核心 效果 JQueryHTML 遍历 Event事件
JQuery 常用API 参考资料:JQuery 官网 jQuery API 中文文档 核心 jQuery 对象 jQuery() 返回匹配的元素集合,无论是通过在DOM的基础上传递的参数还是创建 ...
- [.NET] C# 知识回顾 - Event 事件
C# 知识回顾 - Event 事件 [博主]反骨仔 [原文]http://www.cnblogs.com/liqingwen/p/6060297.html 序 昨天,通过<C# 知识回顾 - ...
- Event事件
妙味课堂-Event事件 1.焦点:当一个元素有焦点的时候,那么他就可以接受用户的输入(不是所有元素都能接受焦点) 给元素设置焦点的方式: 1.点击 2.tab 3.js 2.(例子:输入框提示文字) ...
- JS学习笔记9之event事件及其他事件
-->鼠标事件-->event事件对象-->默认事件-->键盘事件(keyCode)-->拖拽效果 一.鼠标事件 onclick ---------------鼠标点击事 ...
- WPF:自定义路由事件的实现
路由事件通过EventManager,RegisterRoutedEvent方法注册,通过AddHandler和RemoveHandler来关联和解除关联的事件处理函数:通过RaiseEvent方法来 ...
随机推荐
- 分布式部署下的报表调用 API调用 权限问题以及性能方案
背景描述: 客户的实际情况是需要在具体系统构架前,通过与厂商讨论确定最终的系统架构方案. 需求是客户自己有管理系统,希望建立一个独立的报表服务器,该报表服务器可以对多个管理系统提供报表服务,不知 ...
- springboot学习入门之二---配置文件解析
2springboot配置文件解析 2.1application.properties配置文件 使用application.properties全局配置文件(位置为src/main/resources ...
- log4j配置详解(非常详细)
Log4J的配置文件(Configuration File)就是用来设置记录器的级别.存放器和布局的,它可接key=value格式的设置或xml格式的设置信息.通过配置,可以创建出Log4J的运行环境 ...
- 解决tableView中cell动态加载控件的重用问题
解决tableView中cell动态加载控件的重用问题 tableView的cell,有时候需要在运行时取得对应的数据后才能够动态的创建该cell中的控件并加载到该cell中,此时,你一定会遇到重用问 ...
- python 统计学的各种检验
1.使用python中的Numpy进行t检验 http://www.atyun.com/7476.html 2.scipy中的卡方检验 http://wiki.mbalib.com/wiki/%E5% ...
- ORACLE分区表操作
ORACLE分区表的操作应用 摘要:在大量业务数据处理的项目中,可以考虑使用分区表来提高应用系统的性能并方便数据管理,本文详细介绍了分区表的使用. 在大型的企业应用或企业级的数据库应用中,要处理的数据 ...
- NLog写入Mongo日志配置
Web网站中引入了NLog日志,日志记录在Mongo数据库中,经过两天的简单学习,现简要记录说明下: 首先贴出NLog的学习地址: https://github.com/NLog/NLog/wiki/ ...
- jquery根据字符切割字符串
var str=new String(); var arr=new Array(); str="ddd,dsd,3,dd,g,k"; //可以用字符或字符串分割 arr=str.s ...
- python第三课——数据类型2
day03: 1.列表:list 特点:有序的(有索引.定义和显示顺序是一致的).可变的(既可以改变元素内容也可以自动扩容).可重复的. 可以存储任何的数据类型数据 定义个列表如下: lt = ['宋 ...
- 1812: [Ioi2005]riv
1812: [Ioi2005]riv Time Limit: 10 Sec Memory Limit: 64 MB Submit: 635 Solved: 388 [Submit][Status][D ...