@@ -31,6 +31,7 @@ export default class AtIndexes extends React.Component<
3131 private listId : string
3232 private timeoutTimer : NodeJS . Timeout | number | undefined
3333 private listRef : any
34+ private indexMap : { key : string ; startHeight : number ; endHeight : number } [ ]
3435
3536 public constructor ( props : AtIndexesProps ) {
3637 super ( props )
@@ -39,7 +40,8 @@ export default class AtIndexes extends React.Component<
3940 _scrollTop : 0 ,
4041 _tipText : '' ,
4142 _isShowToast : false ,
42- isWEB : Taro . getEnv ( ) === Taro . ENV_TYPE . WEB
43+ isWEB : Taro . getEnv ( ) === Taro . ENV_TYPE . WEB ,
44+ currentIndex : - 1
4345 }
4446 // 右侧导航高度
4547 this . menuHeight = 0
@@ -50,6 +52,7 @@ export default class AtIndexes extends React.Component<
5052 // 当前索引
5153 this . currentIndex = - 1
5254 this . listId = isTest ( ) ? 'indexes-list-AOTU2018' : `list-${ uuid ( ) } `
55+ this . indexMap = [ ]
5356 }
5457
5558 private handleClick = ( item : Item ) : void => {
@@ -136,23 +139,65 @@ export default class AtIndexes extends React.Component<
136139 }
137140 }
138141
139- private initData ( ) : void {
142+ private async initData ( ) : Promise < void > {
140143 delayQuerySelector ( '.at-indexes__menu' ) . then ( rect => {
141144 const len = this . props . list . length
142145 this . menuHeight = rect [ 0 ] . height
143146 this . startTop = rect [ 0 ] . top
144147 this . itemHeight = Math . floor ( this . menuHeight / ( len + 1 ) )
145148 } )
149+
150+ const headerHeight =
151+ ( await delayQuerySelector ( '#at-indexes__top' ) ) ?. [ 0 ] ?. height || 0
152+ const itemHeight =
153+ ( await delayQuerySelector ( '.at-list__item' ) ) ?. [ 0 ] . height || 0
154+ const titleHeight =
155+ ( await delayQuerySelector ( '.at-indexes__list-title' ) ) ?. [ 0 ] . height || 0
156+
157+ this . indexMap = [ ]
158+ this . props . list . forEach ( ( dataList , i ) => {
159+ if ( i === 0 ) {
160+ this . indexMap . push ( {
161+ key : dataList . key ,
162+ startHeight : headerHeight ,
163+ endHeight :
164+ dataList . items . length * itemHeight + headerHeight + titleHeight
165+ } )
166+ } else {
167+ const prev = this . indexMap [ i - 1 ]
168+ this . indexMap . push ( {
169+ key : dataList . key ,
170+ startHeight : prev . endHeight ,
171+ endHeight :
172+ prev . endHeight + dataList . items . length * itemHeight + titleHeight
173+ } )
174+ }
175+ } )
146176 }
147177
148178 private handleScroll ( e : CommonEvent ) : void {
149179 if ( e && e . detail ) {
180+ const scrollTop = e . detail . scrollTop
181+
150182 this . setState ( {
151- _scrollTop : e . detail . scrollTop
183+ _scrollTop : scrollTop
152184 } )
185+
186+ this . getAnchorIndex ( scrollTop )
153187 }
154188 }
155189
190+ // 根据滚动高度,判断当前应该显示的索引值
191+ private getAnchorIndex ( scrollTop : number ) {
192+ const index = this . indexMap . findIndex ( item => {
193+ return scrollTop >= item . startHeight && scrollTop < item . endHeight
194+ } )
195+
196+ this . setState ( {
197+ currentIndex : index
198+ } )
199+ }
200+
156201 public UNSAFE_componentWillReceiveProps ( nextProps : AtIndexesProps ) : void {
157202 if ( nextProps . list . length !== this . props . list . length ) {
158203 this . initData ( )
@@ -173,8 +218,14 @@ export default class AtIndexes extends React.Component<
173218
174219 public render ( ) : JSX . Element {
175220 const { className, customStyle, animation, topKey, list } = this . props
176- const { _scrollTop, _scrollIntoView, _tipText, _isShowToast, isWEB } =
177- this . state
221+ const {
222+ _scrollTop,
223+ _scrollIntoView,
224+ _tipText,
225+ _isShowToast,
226+ isWEB,
227+ currentIndex
228+ } = this . state
178229
179230 const toastStyle = { minWidth : pxTransform ( 100 ) }
180231 const rootCls = classNames ( 'at-indexes' , className )
@@ -184,7 +235,9 @@ export default class AtIndexes extends React.Component<
184235 const targetView = `at-indexes__list-${ key } `
185236 return (
186237 < View
187- className = 'at-indexes__menu-item'
238+ className = { classNames ( 'at-indexes__menu-item' , {
239+ 'at-indexes__menu-item--active' : currentIndex === i
240+ } ) }
188241 key = { key }
189242 onClick = { this . jumpTarget . bind ( this , targetView , i + 1 ) }
190243 >
0 commit comments