mirror of
https://github.com/element-plus/element-plus.git
synced 2024-12-27 03:01:14 +08:00
6503e55277
* refactor(utils-v2): migrate utils * refactor(utils-v2): migrate utils * refactor(utils-v2): migrate utils * refactor(utils): remove * refactor(utils): rename * refactor(utils): move EVENT_CODE to constants * refactor: remove generic
67 lines
1.6 KiB
TypeScript
67 lines
1.6 KiB
TypeScript
import { isClient } from '@vueuse/core'
|
|
|
|
export const isInContainer = (
|
|
el?: Element,
|
|
container?: Element | Window
|
|
): boolean => {
|
|
if (!isClient || !el || !container) return false
|
|
|
|
const elRect = el.getBoundingClientRect()
|
|
|
|
let containerRect: Pick<DOMRect, 'top' | 'bottom' | 'left' | 'right'>
|
|
if (container instanceof Element) {
|
|
containerRect = container.getBoundingClientRect()
|
|
} else {
|
|
containerRect = {
|
|
top: 0,
|
|
right: window.innerWidth,
|
|
bottom: window.innerHeight,
|
|
left: 0,
|
|
}
|
|
}
|
|
return (
|
|
elRect.top < containerRect.bottom &&
|
|
elRect.bottom > containerRect.top &&
|
|
elRect.right > containerRect.left &&
|
|
elRect.left < containerRect.right
|
|
)
|
|
}
|
|
|
|
export const getOffsetTop = (el: HTMLElement) => {
|
|
let offset = 0
|
|
let parent = el
|
|
|
|
while (parent) {
|
|
offset += parent.offsetTop
|
|
parent = parent.offsetParent as HTMLElement
|
|
}
|
|
|
|
return offset
|
|
}
|
|
|
|
export const getOffsetTopDistance = (
|
|
el: HTMLElement,
|
|
containerEl: HTMLElement
|
|
) => {
|
|
return Math.abs(getOffsetTop(el) - getOffsetTop(containerEl))
|
|
}
|
|
|
|
export const getClientXY = (event: MouseEvent | TouchEvent) => {
|
|
let clientX: number
|
|
let clientY: number
|
|
if (event.type === 'touchend') {
|
|
clientY = (event as TouchEvent).changedTouches[0].clientY
|
|
clientX = (event as TouchEvent).changedTouches[0].clientX
|
|
} else if (event.type.startsWith('touch')) {
|
|
clientY = (event as TouchEvent).touches[0].clientY
|
|
clientX = (event as TouchEvent).touches[0].clientX
|
|
} else {
|
|
clientY = (event as MouseEvent).clientY
|
|
clientX = (event as MouseEvent).clientX
|
|
}
|
|
return {
|
|
clientX,
|
|
clientY,
|
|
}
|
|
}
|