I have div in the middle of the screen. The div contains a submit button. When I click "submit" screen location drops to the bottom. I've tried to scroll to the element with window.scrollTo() but it doesn't work. I need to do that from component.ts, I have an event that is called when I press the submit. I will be very grateful for the help.
3 Answers
use the power of view child
@ViewChild('scroll', { read: ElementRef }) public scroll: ElementRef<any>;
ngAfterViewChecked() {
this.scrollBottom()
}
public scrollBottom() {
this.scroll.nativeElement.scrollTop = this.scroll.nativeElement.scrollHeight;
}
html
<div style="height:200px; overflow-y:auto;" #scroll>
<div *ngFor="let i of list">
{{i.name}}
<br>
</div>
</div>
<button (click)="Add()">Add</button>
<button (click)="scrollToTop()">Scroll to top</button>
<button (click)="scrollBottom()">Scroll to bottom</button>
component
import { Component, ElementRef, ViewChild } from '@angular/core';
@Component({
selector: 'my-app',
templateUrl: './app.component.html',
styleUrls: ['./app.component.css']
})
export class AppComponent {
@ViewChild('scroll', { read: ElementRef }) public scroll: ElementRef<any>;
list = [{ name: 'Nam porta orci sed enim imperdiet, et vulputate erat luctus. Integer a tortor blandit, eleifend velit id, eleifend libero. Quisque quis congue ex, vitae suscipit ipsum. Aliquam felis enim, bibendum vel dolor et, dapibus fringilla felis. Sed magna ipsum, congue molestie nisl non, commodo pellentesque felis. Mauris hendrerit rhoncus turpis, at lacinia metus finibus eget. Pellentesque habitant morbi tristique senectus et netus et malesuada fames ac turpis egestas. Aliquam ligula risus, tincidunt ac diam vehicula, fringilla blandit sapien. Ut dictum a nunc id tempus. Nulla sed felis id quam hendrerit volutpat eget at velit. Praesent finibus, mauris ac molestie laoreet, erat est mattis velit, ac tincidunt nulla enim ac felis. In hac habitasse platea dictumst. Nam neque nibh, tempus a velit a, porttitor finibus quam. Nunc pharetra est eget urna mollis, sed facilisis eros pulvinar. Duis ac metus egestas, malesuada ligula eu, congue magna.Nam porta orci sed enim imperdiet, et vulputate erat luctus. Integer a tortor blandit, eleifend velit id, eleifend libero. Quisque quis congue ex, vitae suscipit ipsum. Aliquam felis enim, bibendum vel dolor et, dapibus fringilla felis. Sed magna ipsum, congue molestie nisl non, commodo pellentesque felis. Mauris hendrerit rhoncus turpis, at lacinia metus finibus eget. Pellentesque habitant morbi tristique senectus et netus et malesuada fames ac turpis egestas. Aliquam ligula risus, tincidunt ac diam vehicula, fringilla blandit sapien. Ut dictum a nunc id tempus. Nulla sed felis id quam hendrerit volutpat eget at velit. Praesent finibus, mauris ac molestie laoreet, erat est mattis velit, ac tincidunt nulla enim ac felis. In hac habitasse platea dictumst. Nam neque nibh, tempus a velit a, porttitor finibus quam. Nunc pharetra est eget urna mollis, sed facilisis eros pulvinar. Duis ac metus egestas, malesuada ligula eu, congue magna.Nam porta orci sed enim imperdiet, et vulputate erat luctus. Integer a tortor blandit, eleifend velit id, eleifend libero. Quisque quis congue ex, vitae suscipit ipsum. Aliquam felis enim, bibendum vel dolor et, dapibus fringilla felis. Sed magna ipsum, congue molestie nisl non, commodo pellentesque felis. Mauris hendrerit rhoncus turpis, at lacinia metus finibus eget. Pellentesque habitant morbi tristique senectus et netus et malesuada fames ac turpis egestas. Aliquam ligula risus, tincidunt ac diam vehicula, fringilla blandit sapien. Ut dictum a nunc id tempus. Nulla sed felis id quam hendrerit volutpat eget at velit. Praesent finibus, mauris ac molestie laoreet, erat est mattis velit, ac tincidunt nulla enim ac felis. In hac habitasse platea dictumst. Nam neque nibh, tempus a velit a, porttitor finibus quam. Nunc pharetra est eget urna mollis, sed facilisis eros pulvinar. Duis ac metus egestas, malesuada ligula eu, congue magna.' }];
public Add() {
this.list.push({ name: 'Nam porta orci sed enim imperdiet, et vulputate erat luctus. Integer a tortor blandit, eleifend velit id, eleifend libero. Quisque quis congue ex, vitae suscipit ipsum. Aliquam felis enim, bibendum vel dolor et, dapibus fringilla felis. Sed magna ipsum, congue molestie nisl non, commodo pellentesque felis. Mauris hendrerit rhoncus turpis, at lacinia metus finibus eget. Pellentesque habitant morbi tristique senectus et netus et malesuada fames ac turpis egestas. Aliquam ligula risus, tincidunt ac diam vehicula, fringilla blandit sapien. Ut dictum a nunc id tempus. Nulla sed felis id quam hendrerit volutpat eget at velit. Praesent finibus, mauris ac molestie laoreet, erat est mattis velit, ac tincidunt nulla enim ac felis. In hac habitasse platea dictumst. Nam neque nibh, tempus a velit a, porttitor finibus quam. Nunc pharetra est eget urna mollis, sed facilisis eros pulvinar. Duis ac metus egestas, malesuada ligula eu, congue magna.' });
console.log(this.scroll.nativeElement.scrollHeight);
}
ngAfterViewChecked() {
this.scrollBottom()
}
public scrollBottom() {
this.scroll.nativeElement.scrollTop = this.scroll.nativeElement.scrollHeight;
}
public scrollToTop() {
this.scroll.nativeElement.scrollTop = 0;
}
}
Update 1:
ngAfterViewChecked() {
// this.scrollToTop()
}
public scrollBottom() {
console.log(this.scroll.nativeElement.scrollTop);
this.scroll.nativeElement.scrollTop = this.scroll.nativeElement.scrollHeight;
}
comment out the ngAfterViewChecked() you can able to scroll down
By default, I set it to scroll up after init
Try the same link to view the update source
3 Comments
Edit
I tried to do the same. My screen is still on the bottom. I get the same behavior in your slackblitz link
Pandiyan Cool
@AndriiKolesnyk By default, I set it to scroll up after init Try the same link to view the update source. I have updated the answer for showing the reason of latest change
Pandiyan Cool
If you think the answer is helpful, feel free to accept the answer and upvote it.
I found another approach to do that. It may be helpful for someone so here is another way:
component.ts
someEvent(event: any): void
{
this.scrollToAnchor('scrollAnchor');
}
scrollToAnchor(divId: string): void
{
const element = document.getElementById(divId);
if (element)
{
setTimeout(() => {
element.scrollIntoView(true);
}, 0);
}
}
component.html
<div id="scrollAnchor"></div>