parent.ts 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. import { computed, onMounted, ref } from 'vue'
  2. /**
  3. * 获取真实DOM的高度
  4. * @returns [heightRef, VMRef, readyRef]
  5. */
  6. export const getVMDomWH = (attr: string): any[] => {
  7. const origin = ref<number>(0)
  8. const domRef = ref<HTMLElement | null>(null)
  9. const ready = ref(false)
  10. const referWH = () => {
  11. origin.value = 0
  12. ready.value = false
  13. if (domRef.value) {
  14. const offsetWidth = domRef.value.offsetWidth
  15. const offsetHeight = domRef.value.offsetHeight
  16. setTimeout(() => {
  17. origin.value = attr === 'width' ? offsetWidth : offsetHeight
  18. setTimeout(() => (ready.value = true))
  19. })
  20. }
  21. }
  22. onMounted(referWH)
  23. return [origin, domRef, ready, referWH]
  24. }
  25. /**
  26. * 生成切换高度的方法
  27. * @returns [VMRef, fn, maxHeightRef, originHeightRef, showRef, readyRef]
  28. */
  29. export const changeWHFactory = (isShow = false, attr = 'height') => {
  30. const [origin, domRef, ready, referWH] = getVMDomWH(attr)
  31. const max = ref(0)
  32. const show = computed({
  33. get: () => {
  34. return max.value == origin.value
  35. },
  36. set: (val) => {
  37. max.value = val ? origin.value : 0
  38. },
  39. })
  40. const changeShow = (val = !show.value) => {
  41. show.value = val
  42. }
  43. onMounted(() => {
  44. show.value = isShow
  45. })
  46. return [domRef, changeShow, max, origin, show, ready, referWH]
  47. }
  48. /**
  49. * 获取父级滚动
  50. * @param {*} node
  51. * @returns
  52. */
  53. export const getScrollParent = (node: HTMLElement): HTMLElement | null => {
  54. if (node == null) {
  55. return null
  56. }
  57. if (node === document.documentElement) {
  58. return node
  59. }
  60. const cssScrollY = getComputedStyle(node).overflowY
  61. const cssScrollX = getComputedStyle(node).overflowX
  62. if (
  63. node.scrollHeight > node.clientHeight ||
  64. cssScrollY === 'auto' ||
  65. cssScrollY === 'scroll' ||
  66. cssScrollX === 'auto' ||
  67. cssScrollX === 'scroll'
  68. ) {
  69. return node
  70. } else {
  71. return getScrollParent(node.parentNode as HTMLElement)
  72. }
  73. }
  74. /**
  75. * 获取所有父级滚动
  76. * @param {*} origin
  77. * @param {*} target
  78. */
  79. export const getScrollParents = (origin: any, target: HTMLElement) => {
  80. const parents: HTMLElement[] = []
  81. let temporary = origin
  82. while (
  83. temporary &&
  84. temporary !== target &&
  85. temporary !== document.documentElement &&
  86. target.contains(temporary)
  87. ) {
  88. const scrollParent = getScrollParent(temporary)
  89. if (scrollParent) {
  90. if (scrollParent !== origin) {
  91. parents.push(scrollParent)
  92. }
  93. temporary = scrollParent.parentNode
  94. } else {
  95. break
  96. }
  97. }
  98. return parents
  99. }