Today we are going to discuss about the ionic scroll to top or bottom button or go to top button.
Some time with a large list of data, we want a button to quickly scroll our page to the top like in websites.
We will build the go to top button or ionic scroll to top button like this,
So, it is quite easy task.
Read Also : Angular Material Design With Ionic 4
Let’s start,
First thing is to understand the ion-content component of ionic. The ion-content basically provide the simple wrapper for other ionic components and content area with lot of useful methods and events.
Following are the simple steps to achieve the go to top button,
First step is to achieve the go to top button or scroll to top or bottom button is the scroll-events property of ion-content. This property allows ion-content to listen the scroll events like scroll-start, scroll-end and apply scroll methods to scroll the page to the particular position of the page.
<ion-content [scrollEvents]="true">
Get the ion-content component by using ViewChild decorator into our typescript file.
ViewChild(IonContent) content: IonContent;
Import the platform to get the viewport height.
import { IonContent, Platform } from '@ionic/angular';
Create a Boolean variable to show and hide the go to top button.
backToTop: boolean = false;
Add the ionScroll event to the ion-content component.
<ion-content [scrollEvents]="true" (ionScroll)="getScrollPos($event.detail.scrollTop)">
Define the method to get the scrolling position of the page.
getScrollPos(pos: number) { if (pos > this.platform.height()) { this.backToTop = true; } else { this.backToTop = false; } }
Specify the go to top method with scrolling speed.
gotToTop() { this.content.scrollToTop(1000); }
It’s time to design the go-to top button and add the click event to use go to the top method.
<div class="back-to-top-btn" *ngIf="backToTop" (click)="gotToTop()"> <ion-icon name="chevron-up-outline"></ion-icon> </div>
.back-to-top-btn { background: #000000; position: fixed; border-radius: 50%; width: 30px; height: 30px; color: #fff; display: flex; justify-content: center; align-items: center; left: 50%; transform: translateX(-50%); bottom: 65px; z-index: 999; }
For ionic scroll to bottom button you just need another button like we have designed for go to top button. We will show this button when backToTop : false
and we will hide this go to bottom button when backToTop: true
That’s it, This is the tutorial about ionic scroll to top or bottom button. If you have any query please let me know in the comment section below.
Today we are going to learn about managing multiple PHP versions on ubuntu with xampp.…
Let's understand about how to use coding to improve your website's SEO. In today’s computerized…
Let's understand the most important linux commands for web developers. Linux, as an open-source and…
Today we are going to discuss top 75+ Laravel interview questions asked by top MNCs.Laravel,…
Today we will discuss about the Mailtrap integration with laravel 10 .Sending and receiving emails…
Today we are going to integrate FCM (Firebase Cloud Messaging) push notifications with ionic application.Firebase…
View Comments