Home

Awesome

NPM version

ngx-hm-carousel

A lightweight carousel UI for Angular, support mobile touch with Hammerjs.

Work with custom animation, and server-side-rendering.

Description

An Carousel that easy to use with your custom template.

This package is design by angular and hammerjs.

Depend on Hammerjs.

Support Angular 18+ please use v18.x.x version, which follow the main version of Angular.

for version before v18, please use v3.0.0, view more legacy version in legacy readme.

Stackblitz Example

Stackblitz

Install

npm install --save ngx-hm-carousel hammerjs

Example

import 'hammerjs';

import { provideExperimentalZonelessChangeDetection, model } from '@angular/core';

import { ChangeDetectionStrategy, Component } from '@angular/core';
import { bootstrapApplication } from '@angular/platform-browser';
import { NgxHmCarouselComponent, NgxHmCarouselDynamicDirective, NgxHmCarouselItemDirective } from './lib';
import { NgClass, NgFor } from '@angular/common';
import { FormsModule } from '@angular/forms';

@Component({
  selector: 'app-root',
  standalone: true,
  imports: [NgxHmCarouselComponent, NgxHmCarouselItemDirective, NgxHmCarouselDynamicDirective, NgClass, FormsModule, NgFor],
  templateUrl: './app.component.html',
  changeDetection: ChangeDetectionStrategy.OnPush,
})
export class App {
  currentIndex = model(0);
  speed = 5000;
  infinite = true;
  direction = 'right';
  directionToggle = true;
  autoplay = true;
  avatars = '1234567891234'.split('').map((x, i) => {
    const num = i;
    // const num = Math.floor(Math.random() * 1000);
    return {
      url: `https://picsum.photos/600/400/?${num}`,
      title: `${num}`,
    };
  });

  click(i: number) {
    console.log(`${i}`);
  }
}

bootstrapApplication(App, {
  providers: [provideExperimentalZonelessChangeDetection()],
});

app.component.html

<ngx-hm-carousel [(ngModel)]="currentIndex" [show-num]="4" [autoplay-speed]="speed" [infinite]="infinite" [drag-many]="true" [aniTime]="200" [data]="avatars" class="select-none">
  <section ngx-hm-carousel-container class="flex">
    <article class="transition-opacity duration-200 ease-linear py-2 px-1" ngx-hm-carousel-item *ngFor="let avatar of avatars; let i = index" [ngClass]="currentIndex() === i ? 'opacity-100' : 'opacity-50'">
      <div class="h-96 bg-cover bg-center cursor-pointer" (click)="click(i)" [style.backgroundImage]="'url(' + avatar.url + ')'">{{ i }}</div>
    </article>
    <ng-template #infiniteContainer></ng-template>
  </section>

  <!-- only using in infinite mode or autoplay mode, that will render with-->
  <ng-template #carouselContent let-avatar let-i="index">
    <article class="transition-opacity duration-200 ease-linear py-2 px-1" [ngClass]="currentIndex() === i ? 'opacity-100' : 'opacity-50'">
      <div class="h-96 bg-cover bg-center cursor-pointer" (click)="click(i)" [style.backgroundImage]="'url(' + avatar.url + ')'">{{ i }}</div>
    </article>
  </ng-template>

  <ng-template #carouselPrev>
    <div class="w-[50px] text-center">
      <i class="material-icons text-3xl">keyboard_arrow_left</i>
    </div>
  </ng-template>
  <ng-template #carouselNext>
    <div class="w-[50px] text-center">
      <i class="material-icons text-3xl">keyboard_arrow_right</i>
    </div>
  </ng-template>

  <ng-template #carouselDot let-model>
    <div class="size-2.5 opacity-50 rounded-full border-2 border-solid bg-blue-600" [ngClass]="{ 'opacity-100': model.index === model.currentIndex }"></div>
  </ng-template>

  <ng-template #carouselProgress let-progress>
    <div class="progress"></div>
  </ng-template>
</ngx-hm-carousel>

Input and Output

AttributeNecessaryDefault valueTypeLocationDescription
autoplaynofalsebooleanngx-hm-carouselcarousel auto play confing
autoplay-speedno5000 (ms)numberngx-hm-carouselauto play speed
between-delayno8000 (ms)numberngx-hm-carouseleach auto play between time
autoplay-directionno'right''left' or 'right'ngx-hm-carouselauto play direction
mouse-enablenofalsebooleanngx-hm-carouselis mouse moveover stop the auto play
autoplaynofalsebooleanngx-hm-carouselcarousel auto play config
[breakpoint]no[]NgxHmCarouselBreakPointUpngx-hm-carouselswitch show number with own logic like bootstrap scss media-breakpoint-up
show-numno1number or 'auto'ngx-hm-carouselhow many number items to show once
scroll-numno1numberngx-hm-carouselhow many number with each scroll
drag-manynofalsebooleanngx-hm-carouselis can scroll many item once, simulate with scrollbar
swipe-velocityno0.3numberngx-hm-carouselMinimal velocity required before recognizing, unit is in px per ms.
pan-boundaryno0.15number of falsengx-hm-carouseluser move picture with the container width rate, when more than that rate, it will go to next or prev, set false will never move with distance rate
alignno'left''left' or 'right' or 'center'ngx-hm-carouselwhen show-num is bigger than 1, the first item align
infinitenofalsebooleanngx-hm-carouselis the carousel will move loop
datanoundefinedany[]ngx-hm-carouselthe data you using with *ngFor, it need when infinite mode or autoplay mode
aniTimeno400numberngx-hm-carouselwhen infinite is true, the animation time with item
aniClassno'transition'stringngx-hm-carouselthis class will add when carousel touch drag or click change index
aniClassAutono'aniClass'stringngx-hm-carouselthis class will add when carousel auto play
disable-dragnofalsebooleanngx-hm-carouseldisable drag event with touch and mouse pan moving
not-follow-pannofalsebooleanngx-hm-carouseldisable when drag occur the child element will follow touch point.
[(ngModel)]no0numberngx-hm-carouselYou can bind ngModel with this carousel, it will two way binding with current index. You also can use (ngModelChange)="change($event)" with that.
// the breakpoint interface
export interface NgxHmCarouselBreakPointUp {
  width: number;
  number: number;
}

Other Directive

normal click with effect the touch event, using this event replace that.

AttributeLocationDescription
ngxHmCarouselDynamicany tagIt will dynamic load tag with element.

This Directive will Dynamic load element with previous element and next element and current element.

<section ngx-hm-carousel-container class="content">
  <article class="item cursor-pointer" ngx-hm-carousel-item *ngFor="let item of data; let i = index">
    <div *ngxHmCarouselDynamic="i; length: data.length; index: currentI" class="img" [style.backgroundImage]="item.url"></div>
  </article>
</section>
  1. first data is this data index
  2. length is ths total length with array
  3. index is now index