Creating an Intro Slider for Your Ionic 2 App

The ion-slides component available with Ionic 2 makes it easy to create an intro slider for your app. This can be used as a quick tutorial on how to use the app or a showcase of the features.

Implementing the intro slider is very straightforward, with the only part that can be a bit more tricky being a way to show that intro slider only once, the first time the app is opened. For that, we’ll use Ionic Storage.

Adding an Intro Page

You can use the Ionic CLI to add the Intro page:

$ ionic g page Intro

Then import the new page in your app module and add it to the declarations and entryComponents. Also make sure that you’re importing Storage and including it in the list of providers:

app.module.ts

// ...
import { Storage } from '@ionic/storage';
import { IntroPage } from '../pages/intro/intro';

@NgModule({
  declarations: [
    // ...
    IntroPage
  ],
  imports: [
    IonicModule.forRoot(MyApp),
  ],
  bootstrap: [IonicApp],
  entryComponents: [
    // ...
    IntroPage
  ],
  providers: [
    Storage,
    {provide: ErrorHandler, useClass: IonicErrorHandler }
  ]
})

The Home Page Component

In this example Home.ts is our default home page component, where users will be taken after viewing the intro slider or once they’ve seen the intro slider once already.

Go ahead and import the Intro page, NavController and Storage in your home page component. You should also inject NavController and Storage in the constructor:

home.ts

// ...
import { NavController } from 'ionic-angular';
import { Storage } from '@ionic/storage';
import { IntroPage } from '../pages/intro/intro';

constructor (public navCtrl: NavController, public storage: Storage) {}

// ...

And inside the home page component class let’s use Ionic’s ionViewDidLoad hook to check if an intro-done key has been set in storage. If it hasn’t, we set it so that the intro page doesn’t get shown again and then use NavController to show the intro page with our slider. Notice how we use navCtrl.setRoot instead of navCtrl.push, to make sure that our intro page doesn’t get a back button:

home.ts

ionViewDidLoad() {
  this.storage.get('intro-done').then(done => {
    if (!done) {
      this.storage.set('intro-done', true);
      this.navCtrl.setRoot(IntroPage);
    }
  });
}

Intro Page with Slider Component

The setup work is now done and we can focus on the slider and the intro page itself. We define the slider with the ion-slides component and the individual slides with ion-slide:

<ion-content>
  <ion-slides pager="true" parallax="true" padding>

    <ion-slide>
      <img src="assets/img1.svg">
      <h1>Welcome to my app!</h1>
    </ion-slide>

    <ion-slide>
      <img src="assets/img2.svg">
      <h1>All of the features</h1>
      <p>Here's what you can do with the app...</p>
    </ion-slide>

    <ion-slide>
      <img src="assets/img3.svg">
      <h1>Get started now!</h1>
      <button ion-button outline small (click)="navHome()">
        Start using the app
      </button>
    </ion-slide>

  </ion-slides>
</ion-content>

The ion-slides component can take a bunch of different options. Here we set pager and parallax to true. Pager adds small bullet at the bottom of the slides to show the number of slides and the currently active slide. You can refer to the official documentation for a list of all the options.

Note that the way the options are passed to ion-slides is new since Ionic 2 RC5, and will be the way to go forward.

Notice how we don’t have any navbar on the page, to let the slider take the full height of the page.

Going Back to the Home Page

The button on the last slide calls a navHome method that will set the Home page back as the root page. Simply define the method in the Intro page component class, making sure that NavController has been imported and injected in the constructor and that the Home page is imported:

intro.ts

// ...
import { NavController } from 'ionic-angular';
import { HomePage } from '../home/home';

// ...
export class IntroPage {
  constructor(public navCtrl: NavController) {}

  navHome() {
    this.navCtrl.setRoot(HomePage);
  }
}

A Touch of Style

Once final touch, the pager bullets for the slider are blue by default, so here we changed their color with a small addition to the Sass file of the Intro page:

intro.scss

page-intro {
  .swiper-pagination-bullet-active {
    background: #A713E8;
  }
}
✖ Clear

🕵 Search Results

🔎 Searching...