@ryanuo/utils
Version:
提供多种实用工具函数,涵盖算法、浏览器操作、网络请求等多个领域
61 lines (60 loc) • 2.19 kB
TypeScript
/**
* 批量操作元素类名
* @category DOM
* @example
* ```ts twoslash
* import { manageClasses } from '@ryanuo/utils'
* const el = document.getElementById('myElement') as HTMLElement
* manageClasses(el, 'add', ['class1', 'class2'])
* ```
* @param el 目标元素
* @param action 'add' | 'remove' | 'toggle'
* @param classes 类名数组
*/
export declare function manageClasses(el: HTMLElement, action: 'add' | 'remove' | 'toggle', classes: string[]): void;
/**
* 只触发一次的事件监听
* @category DOM
* @example
* ```ts twoslash
* import { onceEventListener } from '@ryanuo/utils'
* onceEventListener(document, 'click', (e) => {
* console.log(e)
* })
* ```
* @param target 目标元素
* @param event 事件名
* @param handler 处理函数
*/
export declare function onceEventListener(target: EventTarget, event: string, handler: EventListener): void;
/**
* Check if the user is visiting via a mobile device.
* @category DOM
* @example
* ```ts twoslash
* import { isMobile } from '@ryanuo/utils'
* if (isMobile()) {
* console.log('This is a mobile device.')
* }
* ```
* This function determines the device type by detecting the User Agent. It uses regular expressions to search for typical identifiers of mobile devices.
* If the User Agent contains keywords such as Android, webOS, iPhone, iPad, iPod, BlackBerry, IEMobile, or Opera Mini,
* it identifies the device as mobile. This information is crucial for providing responsive design and optimizing user experience.
*
* @returns {boolean} Returns true if the user appears to be using a mobile device; otherwise returns false.
*/
export declare function isMobile(): boolean;
/**
* Copy text to clipboard
* @category DOM
* @param text The text to be copied.
* @returns A boolean indicating success or failure of the operation.
*/
export declare function copyToClipboard(text: string): Promise<boolean>;
/**
* Implement fullscreen effect upon clicking any element.
* @category DOM
* @param selector Selector to target elements
* @param event Event to trigger fullscreen, default is 'click'
*/
export declare function enterFullScreen(selector: string, event?: string): void;