normal.ts 1.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041
  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, ...props) {
  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 = (origin, target, isIncludeSelf = false) => {
  25. const pos = { x: 0, y: 0, width: origin.offsetWidth, height: origin.offsetHeight }
  26. let temporary = origin
  27. while (temporary && temporary !== target && temporary !== document.documentElement && target.contains(temporary)) {
  28. pos.x += temporary.offsetLeft + temporary.clientLeft
  29. pos.y += temporary.offsetTop + temporary.clientTop
  30. temporary = temporary.offsetParent
  31. }
  32. if (isIncludeSelf) {
  33. pos.x += pos.width
  34. pos.y += pos.height
  35. }
  36. return pos
  37. }