how to move div scroll position based on button click in angular 2

前端 未结 2 403
星月不相逢
星月不相逢 2021-01-05 08:40

in app.component.html file i have one div element with horizontal scroll and two buttons as Next and Previous. based on these button click i want to move scroll.

2条回答
  •  鱼传尺愫
    2021-01-05 09:22

    I would prefer to use scrollIntoView() method. This method provides smooth scrolling transition effect in the browser when we click on the corresponding element.

        @Component({
        selector: 'my-app',
        template: `
          
    ` }) export class AppComponent { public arr = ['foo', 'bar', 'baz']; @ViewChild('panel') public panel:ElementRef; public moveToSpecificView()(): void { setTimeout(() => { this.panel.nativeElement.scrollIntoView({ behavior: 'smooth', block: 'end', inline: 'start' }); }); } }

提交回复
热议问题