-
-
Notifications
You must be signed in to change notification settings - Fork 99
Smooth Scroll Functions
Murhaf Sousli edited this page Mar 6, 2021
·
9 revisions
The following smooth scroll function returns a promise that resolves when scrolling reaches the point or target
scrollable.scrollTo(options: SmoothScrollToOptions)
- Element: target HTMLElement, or ElementRef or the selector string.
-
Options:
-
Top: Set top offset, default
null
. -
Bottom: Set bottom offset, default
null
. -
Start: Set start offset (evaluates left in ltr, in right rtl), default
null
. -
End: Set end offset (evaluates right in ltr, in left rtl), default
null
. -
Left: Set left offset, default
null
. -
Right: Set right offset, default
null
. -
Duration: Time to reach the target in ms, default
null
. -
Easing: Smooth scroll animation can be customized using the bezier-easing properties
{ x1, y1, x2, y2 }
.
-
Top: Set top offset, default
Avoid setting top
and bottom
, or start
and end
, or left
and right
at the same time because that would obviously not work.
scrollable.scrollToElement(target: HTMLElement | ElementRef | string, options?)
- Element: target HTMLElement, or ElementRef or the selector string.
-
Options:
-
Left: Set left offset, default
null
. -
Right: Set right offset, default
null
. -
Duration: time to reach the position in milliseconds, default
null
. -
Easing: Smooth scroll animation can be customized using the bezier-easing properties
{ x1, y1, x2, y2 }
.
-
Left: Set left offset, default
<ng-scrollbar #scrollbarRef>
<!-- Content -->
</ng-scrollbar>
<button (click)="scrollbarRef.scrollTo({ top: 0 })">Go to top</button>
<ng-scrollbar #scrollable>
<div id="..."></div>
<div id="..."></div>
<div id="..."></div>
<div id="comments"></div>
<div #footer></div>
</ng-scrollbar>
<button (click)="scrollable.scrollToElement('#comments')">Comments Section</button>
<button (click)="scrollable.scrollToElement(footer)">Scroll to footer</button>
<ng-scrollbar #scrollable>
<div #intro></div>
<div #title></div>
<div #body></div>
<div #comments></div>
<div #author></div>
</ng-scrollbar>
<button (click)="scrollable.scrollToElement(comments)">Usage Section</button>
The same can be done from component code using the @ViewChild
decorator
@ViewChild(NgScrollbar, { static: true }) scrollable: NgScrollbar;
@ViewChild('comments', { static: true }) commentsSection: ElementRef;
scrollToCommentsSection() {
this.scrollable.scrollToElement(this.commentsSection);
}
If you wrap the <router-outlet>
inside <ng-scrollbar>
, you can scroll to the top on route changes.
The following example scrolls to top whenever the user navigates to another page.
<ng-scrollbar>
<router-outlet></router-outlet>
</ng-scrollbar>
export class AppComponent {
@ViewChild(NgScrollbar, { static: true }) scrollable: NgScrollbar;
constructor(router: Router) {
router.events.pipe(
filter(event => event instanceof NavigationEnd)),
filter(() => !!this.scrollable)),
tap((event: NavigationEnd) => this.scrollable.scrollTo({ top: 0, duration: 500 }))
).subscribe();
}
}
Become a sponsor and get your logo on our README on GitHub and the front page of https://ngx-scrollbar.netlify.com/.
Become a backer and get your logo on our README on GitHub.
Latest version (v15)
Addons:
Older version (v13)
- Demo for v13
- Usage
- Styling
- Global Options
- Pointer Events
- Scroll Event
- Updated Event
- Smooth Scroll Functions
- Performance tweak
- Integration
- Reached events