• Stars
    star
    102
  • Rank 335,584 (Top 7 %)
  • Language
    TypeScript
  • License
    MIT License
  • Created about 8 years ago
  • Updated over 1 year ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

Use iDangero.us' great slider, Swiper in Angular 2.

ngx-useful-swiper

Use iDangero.us's great slider Swiper in Angular.

Quick links

Install

npm install --save ngx-useful-swiper@latest swiper

Add the swiper styles to the app styles in angular.json.

{
  "$schema": "./node_modules/@angular/cli/lib/config/schema.json",
  "version": 1,
  "newProjectRoot": "projects",
  "projects": {
    "demo": {
      ...
      "architect": {
        "build": {
          "builder": "@angular-devkit/build-angular:browser",
          "options": {
            ...
            "styles": [
              "./node_modules/swiper/swiper-bundle.css",
            ],
            ...

Usage

In app.module.ts (or in whichever child module you are using the component) import the NgxUsefulSwiperModule module.

import { NgxUsefulSwiperModule } from 'ngx-useful-swiper';

@NgModule({
  declarations: [AppComponent],
  imports: [BrowserModule, NgxUsefulSwiperModule],
  providers: [],
  bootstrap: [AppComponent],
})
export class AppModule {}

Add the swiper component to your component to create a slider and add the content as you normally would to set up a slider (see the official demos for more information). Note, you don't need to include the swiper-container div just the content, but the slides should be contained in a swiper-wrapper div and have the class swiper-slide.

<my-component>
  <swiper [config]="config">
    <div class="swiper-wrapper">
      <div class="swiper-slide">Slide 1</div>
      <div class="swiper-slide">Slide 2</div>
      <div class="swiper-slide">Slide 3</div>
      <div class="swiper-slide">Slide 4</div>
      <div class="swiper-slide">Slide 5</div>
      <div class="swiper-slide">Slide 6</div>
      <div class="swiper-slide">Slide 7</div>
      <div class="swiper-slide">Slide 8</div>
      <div class="swiper-slide">Slide 9</div>
      <div class="swiper-slide">Slide 10</div>
    </div>
    <!-- Add Pagination -->
    <div class="swiper-pagination"></div>
    <!-- Add Arrows -->
    <div class="swiper-button-next"></div>
    <div class="swiper-button-prev"></div>
  </swiper>
</my-component>

Set the config for the swiper in you component and bind it to the component config property as above.

import { SwiperOptions } from 'swiper';


export class MyComponent implements OnInit {
config: SwiperOptions = {
    pagination: { el: '.swiper-pagination', clickable: true },
    navigation: {
      nextEl: '.swiper-button-next',
      prevEl: '.swiper-button-prev'
    },
    spaceBetween: 30
  };

Set the height and width of the component.

swiper {
  height: 300px;
  width: 400px;
}

The component also checks for the contents of swiper-wrapper being changed and calls update on the swiper when they are. This allows for dynamic slide lists as you can see from the demo in this repo.

<swiper [config]="config">
  <div class="swiper-wrapper">
    <img class="swiper-slide" *ngFor="let image of images" [src]="image" />
  </div>
  <div class="swiper-pagination"></div>
  <div class="swiper-button-next"></div>
  <div class="swiper-button-prev"></div>
</swiper>

note for Bootstrap users

To ensure the swiper works will with a column layout you may need to set the box-sizing to border-box on the swiper-wrapper.

.swiper-wrapper {
  box-sizing: border-box;
}

Manually initializing the Swiper

By default the Swiper will be created in the AfterViewChecked event of the component. If the swiper is not going to have been rendered at this time (if it is on a hidden tab for example), it is best to handle the initialization manually. To do this use the component's initialize property and only set it's value to true when ready. This will then initialize the Swiper the next time the next AfterViewChecked event is fired to ensure the DOM is ready.

<mat-tabs mat-ripple mat-tab-active-index="0">
  <mat-tab-panel mat-tab-panel-title="Tab1"> </mat-tab-panel>
  <mat-tab-panel mat-tab-panel-title="Tab2" #panel>
    <swiper [config]="config" class="wrap1" [initialize]="panel.isActive">
      <div class="swiper-wrapper wrap1">
        <img
          class="swiper-slide"
          *ngFor="let image of trigger.images"
          [src]="image"
        />
      </div>
      <div class="swiper-pagination"></div>
      <div class="swiper-button-next"></div>
      <div class="swiper-button-prev"></div>
    </swiper>
  </mat-tab-panel>
</mat-tabs>

Accessing the Swiper instance

When a new instance of Swiper is created it is set as a property on the component. You can then access this by using a template reference. For example add the template reference #usefulSwiper

<swiper [config]="config" #usefulSwiper>
  <div class="swiper-wrapper">
    <img class="swiper-slide" *ngFor="let image of images" [src]="image" />
  </div>
  <div class="swiper-pagination"></div>
  <div class="swiper-button-next"></div>
  <div class="swiper-button-prev"></div>
</swiper>

..and then you can use the reference to access the swiper property.

<button (click)="usefulSwiper.swiper.createLoop()">loop</button>

To access the swiper instance and all of it's properties, methods and events use a viewchild to get the component.swiper property.

 @ViewChild('usefulSwiper',{static: false S}) usefulSwiper: SwiperComponent;

 ...

  next() {
    this.usefulSwiper.swiper.slideNext();
  }

More Repositories

1

angular2-highlight-js

highlight.js integration with Angular
TypeScript
21
star
2

Durandal451

Durandal 2.0.1 on ASP.NET 4.5.1 secured with OWIN.
JavaScript
19
star
3

Angular.Net.SecureStarter

An AngularJS starter kit for ASP.NET which implements authentication and authorization with ASP.NET Identity 2.0.0.
JavaScript
19
star
4

ngx-useful-swiper

Use iDangero.us' great slider, Swiper in Angular.
TypeScript
17
star
5

angular2-disqus

A DISQUS integration for Angular2
JavaScript
12
star
6

ngx-express-universal

angular-cli app with universal
TypeScript
9
star
7

themeable-library-sample

How to create an Angular library that supports Angular Material theming
TypeScript
9
star
8

ng9-themeable-library-sample

How to create an Angular 9 library that supports Angular Material theming
TypeScript
8
star
9

ngx-picture

Properly sized images in next generation formats for Angular
TypeScript
8
star
10

ngx-cli-library

creating a library from an angular-cli project
TypeScript
5
star
11

office-addin-with-angular2

Build an Office add-in with Angular 2.
JavaScript
4
star
12

Genfr

A generic repository with fluent API.
C#
2
star
13

WebApiAngularJsAzureUploader

A Web API, AngularJS Azure Storage uploader.
C#
2
star
14

debuggable-universal-server

Angular: how to debug the univsersal Node Express server with Visual Studio Code
TypeScript
2
star
15

ng-configurable-lazy-lib-sample

How to pass configuration options to a lazy loaded, feature module in Angular (>8.0)
TypeScript
1
star
16

settings-and-secrets

Manage settings and keep secrets out of repos.
JavaScript
1
star
17

angular2-base-library

A seed project for publishing Angular2 libraries.
JavaScript
1
star
18

WpfStylableWindow

WPF stylable window.
C#
1
star
19

ngx-abstract-control-as

If you are using strict template checking and getting the following errors in your reactive forms this library can help out.
TypeScript
1
star
20

WebApiAngularJsUploader

A WebApi - AngularJS file uploader sample application
C#
1
star