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


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.


<div id="th-infinite-scroll-tracker" style="overflow-y:scroll; height: 200px;" scrollTracker (scroll)="onScroll($event)" (mouseup)="searchLog($event)">
        <div *ngFor="let log of arr; let i = index" innerHTML="{{log}}" [id]="i"></div>
<button (click)="onPreviousSearchPosition()" [disabled]="disablePreviousBtn">Previous</button>
<button (click)="onNextSearchPosition()" [disabled]="disableNextBtn">Next</button>


@HostListener('scroll', ['$event'])
    this.scrollObject = event;
    this.disableNextBtn = false; = 20 * --this.idCount;
    this.disableNextBtn = false; = 20 * ++this.idCount;

using above code we can move the scroll but we need scroll event object which will get after scrolling manually.
Please suggest me, how to create scroll event object in component class


This is how you scroll the div element –


import { Component, ViewChild, ElementRef } from '@angular/core';
  selector: 'my-app',
  template: `
    <div #panel style="overflow-y:scroll; height: 20px;" >
      <div *ngFor="let log of arr; let i = index" innerHTML="{{log}}" [id]="i"></div>
    <button (click)="onPreviousSearchPosition()">Previous</button>
    <button (click)="onNextSearchPosition()">Next</button>
export class AppComponent { 
  public arr = ['foo', 'bar', 'baz'];
  @ViewChild('panel', { read: ElementRef }) public panel: ElementRef<any>;

  public onPreviousSearchPosition(): void {
    this.panel.nativeElement.scrollTop -= 20;

  public onNextSearchPosition(): void {
    this.panel.nativeElement.scrollTop += 20;

Answered By – rusev

Answer Checked By – David Goodson (AngularFixing Volunteer)

Leave a Reply

Your email address will not be published.