2.0.2 • Published 1 year ago

ngx-image-slider v2.0.2

Weekly downloads
-
License
MIT
Repository
github
Last release
1 year ago

Material Image Slider

build npm version Live demo

About

This is an image slider component built with Angular and Angular Material. Check out the live demo here.

History and Current Status

This project is a fork and continuation of the now-discontinued @ngmodule/material-carousel by Gabriel Sanches created by Gabriel Sanches. Our current focus is to maintain package security and ensure compatibility with the latest Angular version. We welcome pull requests, including bug fixes, upgrades, and enhancements to the component.

Installation

Install the package using npm: Angular version 17.x.x

npm install --save ngx-image-slider

Angular version 16.x.x

npm install --save ngx-image-slider@1.0.0

Importing the Module

In your Angular application's main module (usually app.module.ts), import the NgxImageSliderModule:

import { NgModule } from "@angular/core";
import { NgxImageSliderModule } from "ngx-image-slider";

// ... other imports

@NgModule({
  // ...
  imports: [
    // ... other modules
    NgxImageSliderModule.forRoot(),
    // ... other modules
  ],
  // ...
})
export class AppModule {}

Usage

The component is named ngx-image-slider. Here's how to use it in your component template:

NgxImageSliderComponent

// Import modules
import { NgModule } from '@angular/core';
import { NgxImageSliderModule } from 'ngx-image-slider';

@NgModule({
  // ...
  imports: [
    // ... others module
    NgxImageSliderModule.forRoot(),
  ],
  // ...
})
export class FeatureModule {} // or it could be AppModule

// Component level
import { OverlayContainer } from '@angular/cdk/overlay';
import { Component, ElementRef, QueryList, ViewChildren } from '@angular/core';
import { ThemePalette } from '@angular/material/core';
import { MatSnackBar } from '@angular/material/snack-bar';
import { NgxImageSliderItemComponent, Orientation } from 'ngx-image-slider';

@Component({
  selector: 'app-example',
  templateUrl: './example.component.html',
  styleUrls: ['./example.component.scss'],
})
export class ExampleComponent {
  images = [
    "https://i0.wp.com/sunrisedaycamp.org/wp-content/uploads/2020/10/placeholder.png?ssl=1&i=1",
    "https://i0.wp.com/sunrisedaycamp.org/wp-content/uploads/2020/10/placeholder.png?ssl=1&i=2",
    "https://i0.wp.com/sunrisedaycamp.org/wp-content/uploads/2020/10/placeholder.png?ssl=1&i=3",
    "https://i0.wp.com/sunrisedaycamp.org/wp-content/uploads/2020/10/placeholder.png?ssl=1&i=4",
    "https://i0.wp.com/sunrisedaycamp.org/wp-content/uploads/2020/10/placeholder.png?ssl=1&i=5"
  ];

  constructor() {}
}
<!-- This is just an example! feel free to set attributes -->
<ngx-image-slider
  timings="250ms ease-in"
  maxWidth="auto"
  [slides]="images.length"
  [hideArrows]="false"
  [useKeyboard]="true"
  [useMouseWheel]="false"
  orientation="ltr"
  proportion="50"
>
  <ngx-image-slider-item
    *ngFor="let image of images"
    [image]="image"
    [hideOverlay]="true"
  ></ngx-image-slider-item>
</ngx-image-slider>

Attributes

InputTypeDescriptionDefault value
timingsstringTimings for slide animation.'250ms ease-in'
autoplaybooleanEnable automatic sliding.true
intervalnumberAutoplay's interval in milliseconds.5000
loopbooleanEnable loop through arrows.true
hideArrowsbooleanHide navigation arrows.false
hideIndicatorsbooleanHide navigation indicators.false
colorThemePaletteColor palette from Material.'accent'
maxWidthstringMaximum width.'auto'
maintainAspectRatiobooleanIf true, use proportion to determine height, else slideHeight is used.true
proportionnumberHeight proportion compared to width.25
slideHeightstringExplicit slide height. Used when maintainAspectRatio is false.'100%'
slidesnumberMaximum amount of displayed slides.
useKeyboardbooleanEnable keyboard navigation.true
useMouseWheelbooleanEnable navigation through mouse wheeling.false
orientationOrientationOrientation of the sliding panel.'ltr'
svgIconOverridesSvgIconOverridesOverride default image slider icons with registered SVG icons.
ariaLabelstringImage slider accessible name'Sliding image slider'
lazyLoadbooleanLazy load contentfalse

Size Considerations and Recommendations

By default, maintainAspectRatio is true, which means height is controlled through proportion.

If you want to have an image slider with constant height (regardless of width), you must set maintainAspectRatio to false.

By default, slideHeight is set to 100%, which will not work if the parent element height isn't defined (i.e. relative heights do not work if the parent height is auto). In that case you can use a fixed CSS unit for slideHeight. You can use any valid css height string like 100px or 25vh.

Play around with the demo here to see how you can use this image slider with or without explicit parent height.

With parent elements that have height:auto

  • use proportion if you want an image slider that resizes responsively (this is the default configuration).
  • use maintainAspectRatio="false" and a non-percentage slideHeight if you want a fixed height image slider.
  • DO NOT use relative (%) values for slideHeight; the image slider will not render.

With parent elements that have a set height

  • use maintainAspectRatio="false" if you want a fixed height image slider that fills the parent element (slideHeight is 100% by default).
  • DO NOT use maintainAspectRatio="false" and slideHeight (unless slideHeight="100%"); the image slider will not

How to develop and test this component

Testing

ng test image-slider --watch false

Running the demo application

ng serve demo

2.0.2

1 year ago

1.0.0

1 year ago

2.0.0

1 year ago

0.0.0-beta.4.1

1 year ago

0.0.0-beta.3

1 year ago

0.0.0-beta.2

1 year ago

0.0.0-beta.1

1 year ago