实现一个自定义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方法来 ...
随机推荐
- Android Studio最全插件整理
在Android开发中,合理的使用Android Studio插件不但可以提高开发效率,还能从整体上提高代码的质量.下面就Android开发中常见的一些插件做一个整理. 1,GsonFormatGso ...
- PSP软件开发过程
1. 引言 这是为了编写psp系统的软件需求分析,主要按照提供的相关需求和功能. 1.1 项目风险 风险承担者包括: 任务提出者:承担任务不能完全按照想象的做出,投入等: 软件开发者:可能不能按时交付 ...
- oracle数据泵备份(Expdp命令)
Oracle备份方式主要分为数据泵导出备份.热备份与冷备份三种,今天首先来实践一下数据泵备份与还原.数据泵导出/导入属于逻辑备份,热备份与冷备份都属于物理备份.oracle10g开始推出了数据泵(ex ...
- Mysql ibdata1简述
What is stored in ibdata1? 当启用innodb_file_per_table时,表存储在它们自己的表空间中,但共享表空间仍用于存储其他InnoDB的内部数据: 数据字典也就是 ...
- Oracle EBS 请求
SELECT t.responsibility_id, t.responsibility_key, t.responsibility_name, t.description, t.menu_id, f ...
- MySQL案例09:Last_IO_Error: Got fatal error 1236 from master when reading data from binary log
刚处理完“挖矿”事件,在做最后一个MySQL NBU备份的时候,发现从库有问题,好奇的是怎么主从状态异常没有告警呢?先不管这么多了,处理了这个问题再完善告警内容. 一.错误信息 从库show slav ...
- [C++] 用Xcode来写C++程序[5] 函数的重载与模板
用Xcode来写C++程序[5] 函数的重载与模板 此节包括函数重载,隐式函数重载,函数模板,带参数函数模板 函数的重载 #include <iostream> using namespa ...
- [翻译] UIImageView-Letters
UIImageView-Letters https://github.com/bachonk/UIImageView-Letters An easy, helpful UIImageView cate ...
- October 04th 2017 Week 40th Wednesday
We teach people how to remember, we never teach them how to grow. 我们教会人们如何记忆,却从来不教他们如何成长. Without pr ...
- September 27th 2017 Week 39th Wednesday
We both look up at the same stars, yet we see such different things. 我们仰望同一片星空,却看见了不同的事物. Looking up ...