|
| 1 | +import _ from 'lodash'; |
| 2 | +import { onBeforeUnmount, watch } from 'vue'; |
| 3 | + |
| 4 | +export default (scrollEle, expandScopeGroupMap, list, isShowSelectPanel) => { |
| 5 | + const handleScroll = _.throttle(() => { |
| 6 | + const groupElList = Array.from(scrollEle.value?.querySelectorAll('.group-item') || []); |
| 7 | + const { top: listTop, bottom: listBottom } = scrollEle.value.getBoundingClientRect(); |
| 8 | + const stickyIndexMap = {}; |
| 9 | + groupElList.forEach((groupEl, index) => { |
| 10 | + const { top } = groupEl.getBoundingClientRect(); |
| 11 | + if (top - 32 * index < listTop) { |
| 12 | + groupEl.style.position = 'sticky'; |
| 13 | + groupEl.style.top = `${32 * index}px`; |
| 14 | + groupEl.style.bottom = ''; |
| 15 | + stickyIndexMap[index] = true; |
| 16 | + } |
| 17 | + }); |
| 18 | + _.reverse(groupElList).forEach((groupEl, index) => { |
| 19 | + const { bottom } = groupEl.getBoundingClientRect(); |
| 20 | + if (bottom + 32 * index > listBottom) { |
| 21 | + groupEl.style.position = 'sticky'; |
| 22 | + groupEl.style.top = ''; |
| 23 | + groupEl.style.bottom = `${32 * index}px`; |
| 24 | + stickyIndexMap[groupElList.length - 1 - index] = true; |
| 25 | + } |
| 26 | + }); |
| 27 | + }, 60); |
| 28 | + |
| 29 | + watch(scrollEle, () => { |
| 30 | + if (scrollEle.value) { |
| 31 | + scrollEle.value?.addEventListener('scroll', handleScroll); |
| 32 | + } |
| 33 | + }); |
| 34 | + watch([expandScopeGroupMap, list, isShowSelectPanel], () => { |
| 35 | + setTimeout(() => { |
| 36 | + handleScroll(); |
| 37 | + }, 60); |
| 38 | + }, { |
| 39 | + immediate: true, |
| 40 | + }); |
| 41 | + |
| 42 | + |
| 43 | + onBeforeUnmount(() => { |
| 44 | + scrollEle.value?.removeEventListener('scroll', handleScroll); |
| 45 | + }); |
| 46 | +}; |
0 commit comments