当前位置:网站首页>Vant source code parsing event Detailed explanation of TS event processing global function addeventlistener
Vant source code parsing event Detailed explanation of TS event processing global function addeventlistener
2022-07-05 20:58:00 【The legend of Feng】
Source code
/* eslint-disable no-empty */
/* eslint-disable getter-return */
/* eslint-disable import/no-mutable-exports */
import { isServer } from '.';// Judge whether it is Server side
type EventHanlder = (event?: Event) => void;
/**
* type It's a joint type
* The type name is EventHanlder
* Participation is event The type is Event Event type
* Return value There is no return value, so the type of return value is void
* */
export let supportsPassive = false;
if (!isServer) {
try {
const opts = {};
Object.defineProperty(opts, 'passive', {
get() {
/* istanbul ignore next */
supportsPassive = true;
}
});
window.addEventListener('test-passive', null as any, opts);
// as To assert Assertion refers to the determination that the data is of a certain type
} catch (e) {}
}
/**
* try catch Capture exception
*
* window.addEventListener There are three parameters
* Event type
* Event bound functions
* opts perhaps true,false
* opt
* capture:boolean If it is `true`, Express `listener` It will be propagated to the `EventTarget` Trigger when
* once:boolean Do you want to set single monitor
* passive:boolean Blocking default behavior On or off
*
*
*/
export function on(
target: HTMLElement,// The goal is dom Elements
event: string,// Event type
handler: EventHanlder,// Event handler type
passive = false//
) {
if (!isServer) {
target.addEventListener(
event,
handler,
supportsPassive ? { capture: false, passive } : false
);
/**
* Not on the server
* target It's incoming dom node
* event Event type
* handler Is the execution method
* supportsPassive Do you support passive
* Yes, it is {capture,false,passive}
No is false
*/
}
}
export function off(target: HTMLElement, event: string, handler: EventHanlder) {
/**
* target To whom , Target element
* event Event type Event
* handler addEventListener Bound events
*
*
*
*/
if (!isServer) {
target.removeEventListener(event, handler);
// off Express Cancellation addEventListener Registered global events removeAddEventListener
}
}
export function stopPropagation(event: Event) {
// stopProgapation() Stop the event from bubbling
event.stopPropagation();
}
export function preventDefault(event: Event, isStopPropagation?: boolean) {
/* istanbul ignore else */
/**
* event Event parameters Event
* isStopPropagation? ? Indicates that the parameter is optional and not mandatory Event Bubbling
*/
if (typeof event.cancelable !== 'boolean' || event.cancelable) {
event.preventDefault();
// Blocking default behavior
}
if (isStopPropagation) {
// Stop the event from bubbling
stopPropagation(event);
}
}
addEventListener Details of event monitoring
addEventListener What is the role of
In the front-end world , Binding events generally use onClick perhaps @click Directly in dom Structurally bound Events ,
however One situation is if html Is dynamic , This is the time Directly in dom Structurally binding events becomes difficult to implement , So I need a dynamic to html Bound to events apI, namely addEventListener
addEventListener Parameters of
type Event type
Event handler
boolean perhaps Object
boolean true yes Event Bubbling , false It's event capture
object There are three parameters
- capture Whether to start the capture phase Triggered when propagated to this element
- once Do you want to set single monitor Trigger only once
- passive Whether to block the default behavior
Remove event monitoring removeEventListener
target.removeEeventListener(event,handler)
There are two parameters Event type Event function
边栏推荐
- EN 438-7建筑覆盖物装饰用层压板材产品—CE认证
- Prior knowledge of machine learning in probability theory (Part 1)
- MySQL InnoDB架构原理
- 研學旅遊實踐教育的開展助力文旅產業發展
- Abnova CD81 monoclonal antibody related parameters and Applications
- shell编程100例
- Duchefa p1001 plant agar Chinese and English instructions
- CareerCup它1.8 串移包括问题
- Sophomore personal development summary
- Generics of TS
猜你喜欢

Abbkine trakine F-actin Staining Kit (green fluorescence) scheme

研学旅游实践教育的开展助力文旅产业发展
![最长摆动序列[贪心练习]](/img/e1/70dc21b924232c7e5e3da023a4bed2.png)
最长摆动序列[贪心练习]

Analysis of steam education mode under the integration of five Education

10000+ 代码库、3000+ 研发人员大型保险集团的研发效能提升实践

Phpstudy Xiaopi's MySQL Click to start and quickly flash back. It has been solved

phpstudy小皮的mysql点击启动后迅速闪退,已解决

Norgen AAV extractant box instructions (including features)

Duchefa d5124 md5a medium Chinese and English instructions

教你自己训练的pytorch模型转caffe(二)
随机推荐
当用户登录,经常会有实时的下拉框,例如,输入邮箱,将会@qq.com,@163.com,@sohu.com
序列联配Sequence Alignment
教你自己训练的pytorch模型转caffe(一)
How to renew NPDP? Here comes the operation guide!
培养机器人教育创造力的前沿科技
Binary search
Is it safe to open a stock account by mobile phone? My home is relatively remote. Is there a better way to open an account?
wpf 获取datagrid 中指定行列的DataGridTemplateColumn中的控件
王老吉药业“关爱烈日下最可爱的人”公益活动在南京启动
LeetCode: Distinct Subsequences [115]
SYSTEMd resolved enable debug log
Duchefa MS medium contains vitamin instructions
vant 源码解析 之深层 合并对象 深拷贝
Matplotlib drawing retouching (how to form high-quality drawings, such as how to set fonts, etc.)
当Steam教育进入个性化信息技术课程
Duchefa cytokinin dihydrozeatin (DHZ) instructions
LeetCode_哈希表_困难_149. 直线上最多的点数
Who the final say whether the product is good or not? Sonar puts forward performance indicators for analysis to help you easily judge product performance and performance
leetcode:1139. 最大的以 1 为边界的正方形
Duchefa low melting point agarose PPC Chinese and English instructions