Angular2 How to navigate to certain section of the page identified with an id attribute

前端 未结 3 1666
情书的邮戳
情书的邮戳 2020-11-29 10:14

How to navigate to certain section of the page identified with an id attribute?

Example:

I need to navigate to \"structure\" paragraph on my page

         


        
3条回答
  •  一生所求
    2020-11-29 10:57

    Angular 2: I would prefer to go with scrollIntoView() method scrollIntoView. It would provide smooth scrolling transition effect in the browser.

    HTML Code

    Info

    Lorem ipsum dolor sit amet, consectetur adipiscing elit.

    Structure

    Lorem ipsum dolor sit amet, consectetur adipiscing elit.

  • and in the Component.

      @Component({
        selector: 'my-app',
        template: `template.html        `
    })
    export class AppComponent {
        @ViewChild('structure') public structure:ElementRef;
        @ViewChild('info') public info:ElementRef;
    
        public moveToStructure():void {
                this.structure.nativeElement.scrollIntoView({ behavior: 'smooth', block: 'end', inline: 'start' });
        }
        public infoStruc():void {
            setImmediate(() => {
                this.info.nativeElement.scrollIntoView({ behavior: 'smooth', block: 'end', inline: 'start' });
            });
        }
    }
    

提交回复
热议问题