当前位置:网站首页>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
边栏推荐
- Is it necessary for bazel to learn
- Abnova丨培养细胞总 RNA 纯化试剂盒中英文说明书
- 中国管理科学研究院凝聚行业专家,傅强荣获智库专家“十佳青年”称号
- Duchefa cytokinin dihydrozeatin (DHZ) instructions
- The reason why the ncnn converted model on raspberry pie 4B always crashes when called
- 获取前一天的js(时间戳转换)
- Duchefa MS medium contains vitamin instructions
- 珍爱网微服务底层框架演进从开源组件封装到自研
- Material design component - use bottomsheet to show extended content (II)
- 清除app data以及获取图标
猜你喜欢

2.<tag-哈希表, 字符串>补充: 剑指 Offer 50. 第一个只出现一次的字符 dbc

Chemical properties and application instructions of prosci Lag3 antibody

【案例】元素的显示与隐藏的运用--元素遮罩

Maker education infiltrating the transformation of maker spirit and culture

基於flask寫一個接口

Abnova丨E (DIII) (WNV) 重组蛋白 中英文说明书

重上吹麻滩——段芝堂创始人翟立冬游记

示波器探头对测量带宽的影响

LeetCode_哈希表_困难_149. 直线上最多的点数

珍爱网微服务底层框架演进从开源组件封装到自研
随机推荐
bazel是否有学习的必要
MYSQL IFNULL使用功能
shell编程100例
Abnova CD81 monoclonal antibody related parameters and Applications
Analysis of steam education mode under the integration of five Education
php中explode函数存在的陷阱
ts 之 类的简介、构造函数和它的this、继承、抽象类、接口
Simple getting started example of Web Service
Monorepo管理方法论和依赖安全
Influence of oscilloscope probe on signal source impedance
水泥胶黏剂BS 476-4 不燃性测试
判断横竖屏的最佳实现
Wanglaoji pharmaceutical's public welfare activity of "caring for the most lovely people under the scorching sun" was launched in Nanjing
100 cases of shell programming
线程池的使用
教你自己训练的pytorch模型转caffe(一)
【案例】定位的运用-淘宝轮播图
Use of thread pool
Enclosed please find. Net Maui's latest learning resources
挖财商学院给的证券账户安全吗?可以开户吗?