当前位置:网站首页>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 the securities account given by the school of Finance and business safe? Can I open an account?
- phpstudy小皮的mysql点击启动后迅速闪退,已解决
- poj 3414 Pots (bfs+线索)
- vant 源码解析之 utils/index.ts 工具函数
- How to open an account online for futures? Is it safe?
- XML modeling
- Abbkine trakine F-actin Staining Kit (green fluorescence) scheme
- Mathematical analysis_ Notes_ Chapter 9: curve integral and surface integral
- 【案例】元素的显示与隐藏的运用--元素遮罩
- Web Service简单入门示例
猜你喜欢
台风来袭!建筑工地该如何防范台风!
使用WebAssembly在浏览器端操作Excel
解析五育融合之下的steam教育模式
产品好不好,谁说了算?Sonar提出分析的性能指标,帮助您轻松判断产品性能及表现
Typhoon is coming! How to prevent typhoons on construction sites!
【案例】元素的显示与隐藏的运用--元素遮罩
Influence of oscilloscope probe on signal source impedance
Talk about my fate with some programming languages
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
Chemical properties and application instructions of prosci Lag3 antibody
随机推荐
haas506 2.0开发教程 - 阿里云ota - pac 固件升级(仅支持2.2以上版本)
Specification of protein quantitative kit for abbkine BCA method
AITM 2-0003 水平燃烧试验
PVC 塑料片BS 476-6 火焰传播性能测定
phpstudy小皮的mysql点击启动后迅速闪退,已解决
Open source SPL eliminates tens of thousands of database intermediate tables
Abnova DNA marker high quality control test program
五层网络协议
教你自己训练的pytorch模型转caffe(二)
vant 源码解析 之深层 合并对象 深拷贝
vant 源码解析之 utils/index.ts 工具函数
2. < tag hash table, string> supplement: Sword finger offer 50 The first character DBC that appears only once
国外LEAD美国简称对照表
2.<tag-哈希表, 字符串>补充: 剑指 Offer 50. 第一个只出现一次的字符 dbc
Interpreting the daily application functions of cooperative robots
实现浏览页面时校验用户是否已经完成登录的功能
Is the securities account given by the school of Finance and business safe? Can I open an account?
ts 之 泛型
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
SQL series (basic) - Chapter 2 limiting and sorting data