/** * 遍历对象 * @param obj * @param run * @returns {*} */ export function loopObject(obj: any, run = (obj: any, key: any, value: any) => value) { const loop: any = (record: any) => { if (!record) return if (typeof record !== 'object') return if (Array.isArray(record)) return record.forEach(item => loop(item, run)) Object.entries(record).forEach(([key, value]) => { if (typeof value === 'object') return loop(value) run(record, key, value) }) } loop(obj) return obj } /** * 检测是否有重复字段 * @param dataSource * @param field * @returns {boolean|*} */ export function checkSameField(dataSource: any, field = 'id') { if (!dataSource || dataSource.length <= 1) return false const allFields = dataSource.map((item: any) => item[field]) const fields: any[] = [] for (let f of allFields) { if (fields.includes(f)) return f fields.push(f) } return false } /** * 数组排序方法,Array.prototype.sort() 方法有兼容性问题,不同浏览器表现不同 * @param arr * @param orderBy * @returns {*[]|*} */ export function sort(arr: any[], orderBy = (a: number, b: number) => a - b) { const { length } = arr for (let i = 0; i < length - 1; i++) { for (let j = 0; j < length - i - 1; j++) { if (orderBy(arr[j], arr[j + 1]) > 0) { arr[j] = [arr[j + 1], arr[j + 1] = arr[j]][0]; } } } return arr } /** * 获取地址栏参数,转为对象 * @param str * @returns {{}} */ export function getQuery(str = '') { const query: any = {} const search = str || window.location.href.split('?')[1]; const urlSearchParams = new URLSearchParams(search); for (let key of urlSearchParams.keys()) { query[key] = urlSearchParams.get(key); } return query; } /** * 获取一个元素距离浏览器顶部高度 * @param element * @returns {number | Requireable} */ export function getElementTop(element: { offsetTop: any; offsetParent: any }) { if (!element) return 0; let actualTop = element.offsetTop; let current = element.offsetParent; while (current !== null) { actualTop += current.offsetTop; current = current.offsetParent; } return actualTop; } /** * 获取浏览器滚动条宽度 * @returns {number} */ export function getScrollBarWidth() { let scrollDiv = document.createElement('div'); scrollDiv.style.position = 'absolute'; scrollDiv.style.top = '-9999px'; scrollDiv.style.width = '50px'; scrollDiv.style.height = '50px'; scrollDiv.style.overflow = 'scroll'; document.body.appendChild(scrollDiv); let scrollBarWidth = scrollDiv.offsetWidth - scrollDiv.clientWidth; window.document.body.removeChild(scrollDiv); return scrollBarWidth; } /** * 判断是否有滚动条 * @param element * @param direction * @returns {boolean} */ export function hasScrollBar(element: { scrollHeight: number; clientHeight: number; scrollWidth: number; clientWidth: number }, direction = 'vertical') { if (direction === 'vertical') { return element.scrollHeight > element.clientHeight; } if (direction === 'horizontal') { return element.scrollWidth > element.clientWidth; } } /** * 判断元素是否在可视窗口内 * @param element * @param containerEle * @returns {{}|{containerHeight: number, visible: boolean, containerScrollTop: (*|number|number), elementTop: *, containerShownHeight: *, elementBottom: *}} */ export function elementIsVisible(element: { getBoundingClientRect: () => any }, containerEle = document.documentElement) { if (!element) return {}; const containerHeight = containerEle.clientHeight; const containerScrollTop = containerEle.scrollTop; const elementRect = element.getBoundingClientRect(); const containerRect = containerEle.getBoundingClientRect(); const elementTop = elementRect.top - containerRect.top + containerScrollTop; const elementBottom = elementTop + elementRect.height; const containerShownHeight = containerScrollTop + containerHeight; // 可见 const visible = !(elementTop > containerShownHeight || elementBottom < containerScrollTop); return { visible, elementTop, elementBottom, containerHeight, containerScrollTop, containerShownHeight, }; } /** * 判断是否是手机端 */ export function isMobile() { const userAgentInfo = navigator.userAgent; const mobileAgents = ["Android", "iPhone", "SymbianOS", "Windows Phone", "iPad","iPod"]; let mobile_flag = false; // 根据userAgent判断是否是手机 for (let v = 0; v < mobileAgents.length; v++) { if (userAgentInfo.indexOf(mobileAgents[v]) > 0) { mobile_flag = true break } } const screen_width = window.screen.width const screen_height = window.screen.height // 根据屏幕分辨率判断是否是手机 if (screen_width < 500 && screen_height < 800) { mobile_flag = true } return mobile_flag }