Tutorial

Creating an Intro Slider for Your Ionic 2 App

Published on January 24, 2017
    Default avatar

    By Alligator.io

    Creating an Intro Slider for Your Ionic 2 App

    While we believe that this content benefits our community, we have not yet thoroughly reviewed it. If you have any suggestions for improvements, please let us know by clicking the “report an issue“ button at the bottom of the tutorial.

    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';
    
    
    

    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) {}
    
    

    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;
      }
    }
    

    Thanks for learning with the DigitalOcean Community. Check out our offerings for compute, storage, networking, and managed databases.

    Learn more about us


    About the authors
    Default avatar
    Alligator.io

    author

    Still looking for an answer?

    Ask a questionSearch for more help

    Was this helpful?
     
    1 Comments
    

    This textbox defaults to using Markdown to format your answer.

    You can type !ref in this text area to quickly search our full set of tutorials, documentation & marketplace offerings and insert the link!

    error TS2304: Cannot find name ‘ionViewDidLoad’. [ng] [ng] 32 ionViewDidLoad() { [ng] ~~~~~~~~~~~~~~

    in code:

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

    Try DigitalOcean for free

    Click below to sign up and get $200 of credit to try our products over 60 days!

    Sign up

    Join the Tech Talk
    Success! Thank you! Please check your email for further details.

    Please complete your information!

    Get our biweekly newsletter

    Sign up for Infrastructure as a Newsletter.

    Hollie's Hub for Good

    Working on improving health and education, reducing inequality, and spurring economic growth? We'd like to help.

    Become a contributor

    Get paid to write technical tutorials and select a tech-focused charity to receive a matching donation.

    Welcome to the developer cloud

    DigitalOcean makes it simple to launch in the cloud and scale up as you grow — whether you're running one virtual machine or ten thousand.

    Learn more
    DigitalOcean Cloud Control Panel