ng-media-slider
v1.0.0
Published
Responsive Angular image and video slider with lightbox (images, base64, MP4/WebM, YouTube). Drop-in successor to ng-image-slider and ng-image-slider-v16 for Angular 20, 21 and 22, zoneless or zone.js, with SSR.
Maintainers
Readme
ng-media-slider
A responsive Angular image and video slider with a lightbox. It supports images (including base64), MP4, WebM and Ogg video, and YouTube links.
It's a drop-in successor to ng-image-slider and ng-image-slider-v16. It keeps the same selector, inputs, outputs, methods and CSS class names, and it's built for current Angular:
- standalone components and signals
- works with zone.js and without it (zoneless)
- server-side rendering and hydration
Coming from either package? See MIGRATION.md. Usually the only change is the import path.
Compatibility
| Angular | zone.js | Zoneless | SSR + hydration | |---|---|---|---| | 22.x | ✅ | ✅ | ✅ | | 21.x | ✅ | ✅ | ✅ | | 20.x | ✅ | ✅ | ✅ |
Every release is tested in CI on each of these Angular majors in both modes. Each check is a fresh SSR app that is built, server-rendered, hydrated and driven in a real browser.
Features
- Media: images (jpg, png, gif, webp, avif, svg and base64); MP4, WebM and Ogg video (URL or base64); YouTube links, including Shorts.
- Sliding:
- Finite or infinite mode.
autoSlide, which pauses on hover, while a keyboard user is inside the slider, and while the browser tab is hidden.- Swipe with touch, pen or mouse drag.
- Keyboard: ←/→ move the slider that has focus; Enter or Space opens a tile.
- Lightbox:
- A native modal
<dialog>: focus stays inside it and returns to the tile when it closes. - Escape closes it, and page scroll is locked while it's open.
- Only the current image and its neighbours load.
- A native modal
- Accessibility:
- Labelled buttons and slide names.
- A screen-reader announcement that stays quiet during autoplay.
- Respects the "reduced motion" setting.
- Checked with axe.
- Layout: tile sizes in px or %, right-to-left pages, and lazy loading.
- Theming: CSS custom properties, plus the original class names.
- Dependencies: none besides
tslib.
Install
npm install ng-media-sliderUsage
Standalone component:
import { Component } from '@angular/core';
import { NgImageSliderComponent, SliderItem } from 'ng-media-slider';
@Component({
selector: 'app-gallery',
imports: [NgImageSliderComponent],
template: `<ng-image-slider [images]="images" (imageClick)="onClick($event)" />`,
})
export class Gallery {
images: SliderItem[] = [
{ image: 'assets/1.jpg', thumbImage: 'assets/1_min.jpg', title: 'Sunset', alt: 'Sun setting over the sea' },
{ video: 'https://youtu.be/6pxRHBw-k8M', title: 'A video' },
{ video: 'assets/movie.mp4', posterImage: 'assets/movie_poster.jpg' },
];
onClick(index: number) {}
}NgModule apps can keep importing the module:
import { NgImageSliderModule } from 'ng-media-slider';
@NgModule({ imports: [NgImageSliderModule] })
export class AppModule {}Items
interface SliderItem {
image?: string; // full-size image for the lightbox (URL or data: URI)
thumbImage?: string; // image shown in the slider tile
video?: string; // YouTube URL, or MP4/WebM/Ogg URL or data: URI
posterImage?: string; // tile image for a video
title?: string; // caption; also used as alt when alt is missing
alt?: string; // alternative text; also used as title when title is missing
order?: number; // sort key, used with orderType
}- Tiles show
thumbImage, thenposterImage, thenvideo. A YouTube link shows its thumbnail. - The lightbox shows
image, thenvideo, thenthumbImage.
Pass a new array to update the slides. Adding, removing or replacing items in the same array (push, splice) is also picked up. Changing a property of an existing item isn't: replace the item instead.
API
Inputs
| Input | Type | Default | Description |
|---|---|---|---|
| images | SliderItem[] | [] | Slides. Never modified by the slider. |
| imageSize | { width?, height?, space? } | { width: 205, height: 205, space: 3 } | Tile size. Numbers are px. Strings may be 'Npx' or 'N%': width % is of the slider, height % is of the window. space is the gap on each side of a tile, in px. |
| infinite | boolean | false | Loop instead of stopping at the ends. |
| autoSlide | number \| boolean \| { interval, stopOnHover? } | off | Seconds between slides (1–5); true means 1 s. Only works with infinite. stopOnHover defaults to true. |
| slideImage | number | 1 | Tiles moved per arrow press. |
| animationSpeed | number | 1 | Slide animation in seconds, from 0.1 to 5. |
| showArrow | boolean | true | Show the previous/next arrows. |
| arrowKeyMove | boolean | true | Move with ←/→ (slider and lightbox). Escape always closes the lightbox. |
| globalArrowKeys | boolean | false | Listen for ←/→ anywhere on the page, as the original packages did. By default only the slider that has focus reacts. |
| imagePopup | boolean | true | Open the lightbox when a tile is clicked. |
| paginationShow | boolean | false | Show "N of M" under the slider and in the lightbox. |
| orderType / slideOrderType | 'ASC' \| 'DESC' | 'ASC' | Sort by each item's order. Items without one go last. slideOrderType wins when both are set. |
| defaultActiveImage | number | -1 | Tile marked as selected (selected-image class); showLightbox() opens there. |
| manageImageRatio | boolean | false | Keep image proportions inside tiles instead of cropping. |
| lazyLoading | boolean | false | loading="lazy" on tile images. |
| fallbackImage | { image?, thumbImage? } | none | Shown when a lightbox image (image) or tile image (thumbImage) fails to load. |
| videoAutoPlay | boolean | false | Start a lightbox video when its slide is shown. |
| showVideoControls | boolean | true | Show player controls. Without them, clicking a video plays or pauses it. |
| direction | 'ltr' \| 'rtl' \| 'auto' | 'ltr' | Text direction of captions. |
| altFallbackToTitle | boolean | true | Use title as alt, and the other way round, when one is missing. |
| ariaLabel | string | none | Name of the slider. When set, the slider is a region landmark; otherwise it's a group named "Image slider". |
| prevLabel / nextLabel / closeLabel | string | 'Previous' / 'Next' / 'Close' | Labels for the arrows and the lightbox close button (for translation). |
Outputs
| Output | Payload | When |
|---|---|---|
| imageClick | number | A tile is clicked (index in display order). |
| arrowClick | { action: 'next' \| 'previous', prevDisable?, nextDisable? } | After a slide animation ends. The disable flags are only included when not infinite. |
| lightboxArrowClick | 'lightbox next' \| 'lightbox previous' | The lightbox image changes. |
| lightboxClose | void | The lightbox closes. close() emits it too. |
Methods and read-only state
Get the component with a template reference (#nav) or @ViewChild / viewChild.
| Member | Description |
|---|---|
| next() / prev() | Move like the arrows do. |
| showLightbox() | Open the lightbox at the selected tile (or the first). |
| close() | Close the lightbox and emit lightboxClose. |
| imageOnClick(index) | Act as if a tile was clicked. |
| sliderPrevDisable / sliderNextDisable | The arrows are disabled (at an end, or while sliding). |
| visiableImageIndex | First visible tile (spelled as in the original). |
| totalImages | Number of slides. |
| activeImageIndex | Selected tile, or -1. |
| ligthboxShow | The lightbox is open (spelled as in the original). |
<ng-image-slider [images]="images" [showArrow]="false" #nav />
<button (click)="nav.prev()" [disabled]="nav.sliderPrevDisable">Prev</button>
<span>{{ nav.visiableImageIndex + 1 }} / {{ nav.totalImages }}</span>
<button (click)="nav.next()" [disabled]="nav.sliderNextDisable">Next</button>Styling
The styles are global, as in the original packages, so you can override the original class names: .ng-image-slider, .img-div, .caption, .prev, .next, .disable, .selected-image, .ng-main-pagination, .ng-image-fullscreen-view, .lightbox-wrapper, .close, .popup-pagination and the rest.
Or set CSS custom properties, on ng-image-slider, any ancestor, or :root:
| Property | Default | Applies to |
|---|---|---|
| --nms-arrow-size | 35px | Slider arrows (circle) |
| --nms-arrow-icon-size | same as --nms-arrow-size | Slider arrows (chevron) |
| --nms-arrow-color | inherit | Slider arrows |
| --nms-arrow-background | #fff | Slider arrows |
| --nms-arrow-background-hover | #d4cdcd | Slider arrows |
| --nms-tile-radius | 5px | Tiles and captions |
| --nms-focus-color | #1a73e8 | Focus outlines |
| --nms-loader | spinner GIF | Loading indicator |
| --nms-tile-placeholder | var(--nms-loader) | Tile background while its image loads (none to remove) |
| --nms-slider-background | transparent | Behind the whole strip, including the gaps between tiles |
| --nms-tile-background | transparent | Behind each tile's image |
| --nms-image-fit | cover | cover fills the tile and crops; contain shows the whole image (letterboxed) |
| --nms-lightbox-background | #1f1f1f | Lightbox |
| --nms-lightbox-color | #fff | Lightbox text |
ng-image-slider {
--nms-arrow-background: #222;
--nms-arrow-color: #fff;
--nms-tile-radius: 0;
}
/* Dark gallery: black behind the photos, each photo shown whole */
ng-image-slider {
--nms-slider-background: #000;
--nms-tile-background: #000;
--nms-image-fit: contain;
}Server-side rendering
The slider renders on the server and hydrates in the browser. It measures the page only in the browser, and autoplay only runs there. In zone.js apps, autoplay runs outside the Angular zone, so the app still becomes stable (hydration and service workers wait for that).
License
MIT. Includes styles and icons derived from ng-image-slider (MIT, © 2018 sanjay verma); see THIRD_PARTY_NOTICES.md.
