normal.ts 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. export const randomId = (e = 6): string => {
  2. const t = 'ABCDEFGHJKMNPQRSTWXYZabcdefhijkmnprstwxyz2345678'
  3. const a = t.length
  4. let n = ''
  5. for (let i = 0; i < e; i++) {
  6. n += t.charAt(Math.floor(Math.random() * a))
  7. }
  8. return n
  9. }
  10. export function omit(obj: any, ...props: any[]) {
  11. const result = { ...obj }
  12. props.forEach((prop) => {
  13. delete result[prop]
  14. })
  15. return result
  16. }
  17. /**
  18. * 获取制定dom在相对于目标中的位置
  19. * @param {*} origin 或获取的DOM
  20. * @param {*} target 目标DOM
  21. * @param {*} isIncludeSelf 是否要包含自身宽高
  22. * @returns 位置信息 {x, y}
  23. */
  24. export const getPostionByTarget = (
  25. origin: HTMLElement,
  26. target: Node,
  27. isIncludeSelf = false
  28. ) => {
  29. const pos = {
  30. x: 0,
  31. y: 0,
  32. width: origin.offsetWidth,
  33. height: origin.offsetHeight,
  34. }
  35. let temporary: HTMLElement = origin
  36. while (
  37. temporary &&
  38. temporary !== target &&
  39. temporary !== document.documentElement &&
  40. target.contains(temporary)
  41. ) {
  42. pos.x += temporary.offsetLeft + temporary.clientLeft
  43. pos.y += temporary.offsetTop + temporary.clientTop
  44. temporary = temporary.offsetParent as HTMLElement
  45. }
  46. if (isIncludeSelf) {
  47. pos.x += pos.width
  48. pos.y += pos.height
  49. }
  50. return pos
  51. }