@@ -92,15 +92,23 @@ export class NzAnchorComponent implements OnDestroy, AfterViewInit, OnChanges {
9292 @WithConfig < number > ( )
9393 nzOffsetTop ?: number = undefined ;
9494
95+ @Input ( )
96+ @InputNumber ( undefined )
97+ @WithConfig < number > ( )
98+ nzTargetOffset ?: number = undefined ;
99+
95100 @Input ( ) nzContainer ?: string | HTMLElement ;
101+ @Input ( ) nzCurrentAnchor ?: string ;
96102
97103 @Output ( ) readonly nzClick = new EventEmitter < string > ( ) ;
104+ @Output ( ) readonly nzChange = new EventEmitter < string > ( ) ;
98105 @Output ( ) readonly nzScroll = new EventEmitter < NzAnchorLinkComponent > ( ) ;
99106
100107 visible = false ;
101108 wrapperStyle : NgStyleInterface = { 'max-height' : '100vh' } ;
102109
103110 container ?: HTMLElement | Window ;
111+ activeLink ?: string ;
104112
105113 private links : NzAnchorLinkComponent [ ] = [ ] ;
106114 private animating = false ;
@@ -160,7 +168,8 @@ export class NzAnchorComponent implements OnDestroy, AfterViewInit, OnChanges {
160168 }
161169
162170 const sections : Section [ ] = [ ] ;
163- const scope = ( this . nzOffsetTop || 0 ) + this . nzBounds ;
171+ const offsetTop = this . nzTargetOffset ? this . nzTargetOffset : this . nzOffsetTop || 0 ;
172+ const scope = offsetTop + this . nzBounds ;
164173 this . links . forEach ( comp => {
165174 const sharpLinkMatch = sharpMatcherRegx . exec ( comp . nzHref . toString ( ) ) ;
166175 if ( ! sharpLinkMatch ) {
@@ -195,11 +204,23 @@ export class NzAnchorComponent implements OnDestroy, AfterViewInit, OnChanges {
195204 } ) ;
196205 }
197206
207+ private setActive ( comp ?: NzAnchorLinkComponent ) : void {
208+ const originalActiveLink = this . activeLink ;
209+ const targetComp = ( this . nzCurrentAnchor && this . links . find ( n => n . nzHref === this . nzCurrentAnchor ) ) || comp ;
210+ if ( ! targetComp ) return ;
211+
212+ targetComp . setActive ( ) ;
213+ const linkNode = targetComp . getLinkTitleElement ( ) ;
214+ this . ink . nativeElement . style . top = `${ linkNode . offsetTop + linkNode . clientHeight / 2 - 4.5 } px` ;
215+ this . activeLink = ( comp || targetComp ) . nzHref ;
216+ if ( originalActiveLink !== this . activeLink ) {
217+ this . nzChange . emit ( this . activeLink ) ;
218+ }
219+ }
220+
198221 private handleActive ( comp : NzAnchorLinkComponent ) : void {
199222 this . clearActive ( ) ;
200- comp . setActive ( ) ;
201- const linkNode = comp . getLinkTitleElement ( ) ;
202- this . ink . nativeElement . style . top = `${ linkNode . offsetTop + linkNode . clientHeight / 2 - 4.5 } px` ;
223+ this . setActive ( comp ) ;
203224 this . visible = true ;
204225 this . setVisible ( ) ;
205226 this . nzScroll . emit ( comp ) ;
@@ -226,7 +247,8 @@ export class NzAnchorComponent implements OnDestroy, AfterViewInit, OnChanges {
226247 this . animating = true ;
227248 const containerScrollTop = this . scrollSrv . getScroll ( this . getContainer ( ) ) ;
228249 const elOffsetTop = getOffsetTop ( el , this . getContainer ( ) ) ;
229- const targetScrollTop = containerScrollTop + elOffsetTop - ( this . nzOffsetTop || 0 ) ;
250+ let targetScrollTop = containerScrollTop + elOffsetTop ;
251+ targetScrollTop -= this . nzTargetOffset !== undefined ? this . nzTargetOffset : this . nzOffsetTop || 0 ;
230252 this . scrollSrv . scrollTo ( this . getContainer ( ) , targetScrollTop , {
231253 callback : ( ) => {
232254 this . animating = false ;
@@ -237,7 +259,7 @@ export class NzAnchorComponent implements OnDestroy, AfterViewInit, OnChanges {
237259 }
238260
239261 ngOnChanges ( changes : SimpleChanges ) : void {
240- const { nzOffsetTop, nzContainer } = changes ;
262+ const { nzOffsetTop, nzContainer, nzCurrentAnchor } = changes ;
241263 if ( nzOffsetTop ) {
242264 this . wrapperStyle = {
243265 'max-height' : `calc(100vh - ${ this . nzOffsetTop } px)`
@@ -248,5 +270,8 @@ export class NzAnchorComponent implements OnDestroy, AfterViewInit, OnChanges {
248270 this . container = typeof container === 'string' ? this . doc . querySelector ( container ) : container ;
249271 this . registerScrollEvent ( ) ;
250272 }
273+ if ( nzCurrentAnchor ) {
274+ this . setActive ( ) ;
275+ }
251276 }
252277}
0 commit comments