@@ -21,6 +21,23 @@ export type useScrollWathType = {
2121 direction ?: DirectionType ;
2222} ;
2323
24+ export const getCurIndex = ( scrollTop : number , list : number [ ] ) => {
25+ const length = list . length ;
26+ if ( ! length ) return - 1 ;
27+
28+ for ( let i = 0 ; i < length ; i ++ ) {
29+ if ( scrollTop < list [ i ] ) {
30+ return i - 1 ;
31+ }
32+ }
33+
34+ if ( scrollTop >= list [ length - 1 ] ) {
35+ return list . length - 1 ;
36+ }
37+
38+ return - 1 ;
39+ } ;
40+
2441export const useScrollWatch = ( props : useScrollWathType ) => {
2542 const { ref, list = [ ] , offset, direction = 'y' } = props ;
2643
@@ -40,33 +57,18 @@ export const useScrollWatch = (props: useScrollWathType) => {
4057 const os = typeof item . offset === 'number' ? item . offset : offset || 0 ;
4158 const elm = document . querySelector ( item . href ) ;
4259 if ( ! elm ) return Infinity ;
43- return parent && isWindowScrollParent ( parent )
60+ if ( ! parent ) return Infinity ;
61+ return isWindowScrollParent ( parent )
4462 ? elm . getBoundingClientRect ( ) [ attrMap . leftTop ] -
45- parent ! . getBoundingClientRect ( ) [ attrMap . leftTop ] +
63+ parent . getBoundingClientRect ( ) [ attrMap . leftTop ] +
4664 os
4765 : elm . getBoundingClientRect ( ) [ attrMap . leftTop ] -
48- parent ! . children [ 0 ] . getBoundingClientRect ( ) [ attrMap . leftTop ] +
66+ parent . children [ 0 ] . getBoundingClientRect ( ) [ attrMap . leftTop ] +
4967 os ;
5068 } ) ;
5169 return posList ;
5270 } ;
5371
54- const getCurIndex = ( scrollTop : number , list : typeof posList ) => {
55- const length = list . length ;
56- if ( ! length ) return - 1 ;
57-
58- for ( let i = 0 ; i < length ; i ++ ) {
59- if ( scrollTop < list [ i ] ) {
60- return i - 1 ;
61- }
62- }
63-
64- if ( scrollTop >= list [ length - 1 ] ) {
65- return list . length - 1 ;
66- }
67-
68- return - 1 ;
69- } ;
7072 const refresh = debounce ( ( ) => {
7173 setScrollTop ( getScrollTop ( ) ) ;
7274 setPosList ( getPosList ( ) ) ;
0 commit comments