当前位置:网站首页>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
边栏推荐
- Duchefa s0188 Chinese and English instructions of spectinomycin hydrochloride pentahydrate
- Enclosed please find. Net Maui's latest learning resources
- How to renew NPDP? Here comes the operation guide!
- PVC 塑料片BS 476-6 火焰传播性能测定
- phpstudy小皮的mysql点击启动后迅速闪退,已解决
- Duchefa MS medium contains vitamin instructions
- shell编程100例
- Prior knowledge of machine learning in probability theory (Part 1)
- Duchefa d5124 md5a medium Chinese and English instructions
- 2.<tag-哈希表, 字符串>补充: 剑指 Offer 50. 第一个只出现一次的字符 dbc
猜你喜欢

Duchefa d5124 md5a medium Chinese and English instructions

Influence of oscilloscope probe on measurement bandwidth

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

显示屏DIN 4102-1 Class B1防火测试要求

Abnova DNA marker high quality control test program

Duchefa p1001 plant agar Chinese and English instructions

Abnova total RNA Purification Kit for cultured cells Chinese and English instructions

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

请查收.NET MAUI 的最新学习资源

Duchefa cytokinin dihydrozeatin (DHZ) instructions
随机推荐
如何让化工企业的ERP库存账目更准确
Prior knowledge of machine learning in probability theory (Part 1)
基於flask寫一個接口
Research and development efficiency improvement practice of large insurance groups with 10000 + code base and 3000 + R & D personnel
Graph embedding learning notes
Écrire une interface basée sur flask
2.<tag-哈希表, 字符串>补充: 剑指 Offer 50. 第一个只出现一次的字符 dbc
Binary search
Open source SPL eliminates tens of thousands of database intermediate tables
木板ISO 5660-1 热量释放速率摸底测试
中国管理科学研究院凝聚行业专家,傅强荣获智库专家“十佳青年”称号
phpstudy小皮的mysql点击启动后迅速闪退,已解决
Norgen AAV extractant box instructions (including features)
Use of thread pool
判断横竖屏的最佳实现
AITM 2-0003 水平燃烧试验
Careercup its 1.8 serial shift includes problems
MySQL InnoDB架构原理
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
示波器探头对信号源阻抗的影响