Slides
We recommend Swiper.js if you need a modern touch slider component. It powers our ion-slides
component, but we now recommend that developers use Swiper for Angular directly.
This guide will go over how to get Swiper for Angular set up in your Ionic Framework application. It will also go over any migration information you may need to move from ion-slides
to the official Swiper Angular integration.
#
Getting StartedTo get started, install the Swiper dependency in your project:
#
Swiping with StyleNext, we need to import the base Swiper styles. We recommend importing the styles in the component in which Swiper is being used. This ensures that the styles are only loaded when needed:
Ionic Framework also provides some default styles, as well as the CSS Variables that were used inside of the old ion-slides
. If you would like to continue to use those styles and CSS Variables, be sure to import the ionic-swiper.css
file:
You should also update any selectors to target the correct Swiper classes. If you were targeting ion-slides
, you should target .swiper-container
. If you were targeting ion-slide
, you should target .swiper-slide
.
#
Vanilla CSS (Optional)For developers not using a CSS pre-processor, Swiper also provides the styles bundled together. It is important to note that this will import styles for all modules as well.
#
Using ComponentsSwiper exports two components: Swiper
and SwiperSlide
. The Swiper
component is the equivalent of IonSlides
, and SwiperSlide
is the equivalent of IonSlide
.
These components are registered by adding SwiperModule
to the module of the component you wish to use Swiper in.
#
The IonicSwiper ModuleThere are a few edge cases in Ionic Framework where Swiper may not be able to compute the slider dimensions properly. As a result, we have created the IonicSwiper
module to resolve some of these issues.
To install it, we first need to import the core Swiper library and the IonicSwiper module:
Then we can install the module:
#
Additional ModulesBy default, Swiper for Angular uses the core version of Swiper and does not import any additional modules. To use modules such as Navigation or Pagination, you need to import them first.
ion-slides
automatically included the Pagination, Scrollbar, Autoplay, Keyboard, and Zoom modules. If you used any of these features, be sure to import them in your application.
The following example shows how to install the Navigation and Pagination plugins:
Importing
swiper-bundle.min.css
imports styles for all modules. When using the SCSS or Less styles, you will need to import the styles for each module. See https://swiperjs.com/angular#styles for a full list of stylesheets.#
Properties
Swiper options are provided as props directly on the <swiper>
component rather than via the options
object in ion-slides
.
Let's say in an app with ion-slides
we had the slidesPerView
and loop
options set:
To migrate, we would move these options out of the options
object and onto the <swiper>
component directly as properties:
Below is a full list of property changes when going from ion-slides
to Swiper Angular:
Name | Notes |
---|---|
options | Set each option as a property directly on the <swiper> component. |
mode | For different styles based upon the mode, you can target the slides with .ios .swiper-container or .md .swiper-container |
pager | Use the pagination property instead. Requires installation of the Pagination module. |
scrollbar | You can continue to use the scrollbar property, just be sure to install the Scrollbar module first. |
All properties available in Swiper Angular can be found at https://swiperjs.com/angular#swiper-props.
#
EventsSince the Swiper
component is not provided by Ionic Framework, event names will not have an ionSlide
prefix to them.
Let's say in an app with ion-slides
we used the ionSlideDidChange
event:
To migrate, we would change the name of the event to slideChange
:
Below is a full list of event name changes when going from ion-slides
to Swiper Angular:
ion-slides Event | Swiper Event |
---|---|
ionSlideWillChange | slideChangeTransitionStart |
ionSlideDidChange | slideChangeTransitionEnd |
ionSlideDoubleTap | doubleTap |
ionSlideDrag | sliderMove |
ionSlideNextStart | slideNextTransitionStart |
ionSlideNextEnd | slideNextTransitionEnd |
ionSlidePrevStart | slidePrevTransitionStart |
ionSlidePrevEnd | slidePrevTransitionEnd |
ionSlideReachStart | reachBeginning |
ionSlideReachEnd | reachEnd |
ionSlideTap | tap |
ionSlideTouchStart | touchStart |
ionSlideTouchEnd | touchEnd |
ionSlideTransitionStart | transitionStart |
ionSlideTransitionEnd | transitionEnd |
ionSlidesDidLoad | init |
All events available in Swiper Angular can be found at https://swiperjs.com/angular#swiper-events.
#
MethodsMost methods have been removed in favor of accessing the <swiper>
props directly.
Accessing these properties can be tricky as you want to access the properties on the Swiper instance itself, not your Angular component. To do this, we recommend getting a reference to the Swiper
instance via (swiper)
:
From here, if you wanted to access a property on the Swiper instance you would access this.slides
. For example, if you wanted to check the isBeginning
property, you could do: this.slides.isBeginning
. Make sure this.slides
is defined first though!
Below is a full list of method changes when going from ion-slides
to Swiper Angular:
Name | Notes |
---|---|
getActiveIndex() | Use the activeIndex property instead. |
getPreviousIndex() | Use the previousIndex property instead. |
getSwiper() | Get a reference to the Swiper instance using (swiper) . See example above. |
isBeginning() | Use the isBeginning property instead. |
isEnd() | Use the isEnd property instead. |
length() | Use the slides property instead. (i.e swiperRef.slides.length) |
lockSwipeToNext() | Use the allowSlidesNext property instead. |
lockSwipeToPrev() | Use the allowSlidePrev property instead. |
lockSwipes() | Use the allowSlideNext , allowSlidePrev , and allowTouchMove properties instead. |
startAutoplay() | Use the autoplay property instead. |
stopAutoplay() | Use the autoplay property instead. |
#
EffectsIf you are using effects such as Cube or Fade, you can install them similar to how you installed the other modules:
For more information on effects in Swiper, please see https://swiperjs.com/angular#effects.
#
Wrap UpNow that you have Swiper installed, there is a whole set of new Swiper features for you to enjoy. We recommend starting with the Swiper Angular Introduction and then referencing the Swiper API docs.
#
Where do I file issues?Before opening an issue, please consider creating a post on the Swiper Discussion Board or the Ionic Forum) to see if your issue can be resolved by the community.
If you are running into problems with the Swiper library, new bugs should be filed on the Swiper repo: https://github.com/nolimits4web/swiper/issues
If you are running into problems with the IonicSwiper
module, new bugs should be filed on the Ionic Framework repo: https://github.com/ionic-team/ionic-framework/issues