UNPKG

vtils

Version:

一个面向业务的 JavaScript/TypeScript 实用程序库。

33 lines (31 loc) 794 B
/* eslint-disable prefer-rest-params, prefer-spread */ /** * 绑定事件函数。 * * @public * @param type 事件类型 * @param callback 事件回调 * @param options 事件选项 * @returns 返回事件解绑函数 */ /** * 绑定事件。 * * @public * @param target 事件绑定的目标 * @returns 返回事件绑定函数 * @example * ```typescript * const bindWindowEvent = bindEvent(window) * const unbindClick = bindWindowEvent('click', console.log) * const unbindScroll = bindWindowEvent('scroll', console.log) * ``` */ export function bindEvent(target) { return function (type, callback, options) { target.addEventListener(type, callback, options); return function () { return target.removeEventListener(type, callback, options); }; }; }