slider-pro-js is a vanilla JavaScript slider library for responsive image galleries and content carousels.
It supports swipe navigation, multi-slide layouts, and thumbnail strips with images, text, or HTML content.
Features
- Horizontal, vertical, and right-to-left slide navigation.
- Infinite looping, autoplay, and fade transitions.
- Full-width, full-window, and fullscreen viewing modes.
- Touch, mouse, and keyboard controls.
- Configurable thumbnail placement and navigation arrows.
- Animated or static text and HTML layers.
- Responsive breakpoints and variable-size slides.
- Lazy-loaded images and high-density image sources.
- Automatic playback handling for video slides.
- URL hashes for opening individual slides.
How To Use It
Installation
Load the compiled stylesheet and the core script from a CDN.
<link rel="stylesheet" href="https://unpkg.com/[email protected]/build/slider-pro.css"> <script src="https://unpkg.com/[email protected]/build/slider-pro.js"></script>
Basic Usage
Create the .slider-pro element with a .sp-slides container. Each .sp-slide is one slide, and .sp-image identifies a slide’s main image.
<div id="photo-slider" class="slider-pro">
<div class="sp-slides">
<div class="sp-slide">
<img class="sp-image" src="images/coast.jpg" alt="Rocky coastline">
</div>
<div class="sp-slide">
<img class="sp-image" src="images/mountains.jpg" alt="Mountain range">
</div>
<div class="sp-slide">
<img class="sp-image" src="images/forest.jpg" alt="Forest trail">
</div>
</div>
</div>
<script>
const slider = new SliderPro('#photo-slider', {
width: 900,
height: 500,
arrows: true,
buttons: true,
autoplay: false
});
</script>npm Package and Bundlers
For a modern web project, use the following imports with the #photo-slider markup from Basic Usage.
npm install slider-pro-js
import SliderPro, { Autoplay, Buttons, TouchSwipe } from 'slider-pro-js';
import 'slider-pro-js/css';
const slider = new SliderPro('#photo-slider', {
addOns: [Autoplay, Buttons, TouchSwipe],
width: 900,
height: 500
});Browser ES Module
The jsDelivr ES module endpoint is another installation option. Use the stylesheet and #photo-slider markup from Basic Usage. Put this import inside a <script type="module"> element after the markup in place of the browser-build initialization.
import SliderPro, { Autoplay, Buttons, TouchSwipe } from
'https://cdn.jsdelivr.net/npm/[email protected]/+esm';
new SliderPro('#photo-slider', {
addOns: [Autoplay, Buttons, TouchSwipe]
});Create a Gallery With Thumbnails
Place the .sp-thumbnails container after .sp-slides. The two images in this example have matching thumbnail images. You can also place a .sp-thumbnail inside each slide or use HTML content in a thumbnail.
<div id="gallery" class="slider-pro">
<div class="sp-slides">
<div class="sp-slide">
<img class="sp-image" src="images/item-1.jpg" alt="First product view">
</div>
<div class="sp-slide">
<img class="sp-image" src="images/item-2.jpg" alt="Second product view">
</div>
</div>
<div class="sp-thumbnails">
<img class="sp-thumbnail" src="images/item-1-thumb.jpg" alt="View one">
<img class="sp-thumbnail" src="images/item-2-thumb.jpg" alt="View two">
</div>
</div>
const gallery = new SliderPro('#gallery', {
width: 800,
height: 450,
autoplay: false,
thumbnailsPosition: 'bottom',
thumbnailWidth: 110,
thumbnailHeight: 70,
thumbnailArrows: true
});
Change Settings at Responsive Breakpoints
Replace the gallery initialization above with this configuration to move the thumbnail strip below the slider on narrow screens. Each breakpoint starts from the original settings. Repeat values that must apply at multiple widths.
const gallery = new SliderPro('#gallery', {
width: 960,
height: 480,
thumbnailsPosition: 'right',
thumbnailWidth: 120,
breakpoints: {
800: {
thumbnailsPosition: 'bottom',
thumbnailWidth: 100
},
500: {
thumbnailsPosition: 'bottom',
thumbnailWidth: 65,
thumbnailHeight: 50
}
}
});
Display Animated Text Layers
Insert elements with the .sp-layer class inside a slide. Use layer data attributes for positioning, animation direction, and delays. The .sp-black, .sp-padding, and .sp-rounded classes apply the included layer styles. If the slider width is percentage-based and you need proportional layer scaling, specify a numeric autoScaleReference.
<div class="sp-slide">
<img class="sp-image" src="images/city.jpg" alt="City skyline at dusk">
<h2 class="sp-layer sp-black sp-padding sp-rounded"
data-position="bottomLeft"
data-horizontal="24"
data-vertical="24"
data-show-transition="left"
data-show-duration="500"
data-hide-transition="right">
Explore the city
</h2>
</div>
Lazy-Load Images and Supply Responsive Sources
For the built-in lazy-loading module, place a placeholder in src and the real image in data-src. data-small, data-medium, and data-large select alternative images at the configured slider-width thresholds. Native srcset is also available for responsive image markup.
<div class="sp-slide">
<img class="sp-image"
src="images/placeholder.gif"
data-src="images/landscape.jpg"
data-small="images/landscape-small.jpg"
data-medium="images/landscape-medium.jpg"
data-large="images/landscape-large.jpg"
alt="Landscape at sunrise">
</div>
Display Video Slides
Use .sp-video for an HTML5 video or a YouTube, Vimeo, or Video.js player. Slider Pro expects enablejsapi=1 on a YouTube iframe and api=1 on a Vimeo iframe for its player integration.
<div class="sp-slide">
<video class="sp-video" controls preload="none"
poster="images/video-poster.jpg" width="800" height="450">
<source src="media/demo.mp4" type="video/mp4">
</video>
</div>
Control Slides and Listen for Changes
Use the slider instance from Basic Usage for additional controls. Place this button after the slider element. Slide indexes start at 0, and event.detail includes the selected and previous indexes for gotoSlide.
<button id="next-photo" type="button">Next photo</button>
slider.addEventListener('gotoSlide', (event) => {
console.log('Selected slide:', event.detail.index);
});
document.querySelector('#next-photo').addEventListener('click', () => {
slider.nextSlide();
});Configuration Options
Pass an options object as the second argument to new SliderPro(selector, options).
Core Sizing and Layout
| Option | Default | Description |
|---|---|---|
width | 500 | Slide width in pixels or a CSS length such as '100%' or '100vw'. |
height | 300 | Slide height in pixels or a CSS length. |
responsive | true | Scales slides to the available space. Fixed dimensions can act as maximum dimensions. |
aspectRatio | -1 | Overrides slide height to maintain a given width-to-height ratio. -1 disables this behavior. |
imageScaleMode | 'cover' | Main image scaling: 'cover', 'contain', 'exact', or 'none'. |
centerImage | true | Centers the main image. |
allowScaleUp | true | Permits images to grow beyond their native dimensions. |
autoHeight | false | Matches slider height to the selected slide. |
autoSlideSize | false | Allows variable widths for horizontal slides or variable heights for vertical slides. |
startSlide | 0 | Zero-based index of the initially selected slide. |
shuffle | false | Randomizes slide order during initialization. |
orientation | 'horizontal' | 'horizontal' or 'vertical' slide arrangement. |
forceSize | 'none' | 'none', 'fullWidth', or 'fullWindow'. |
loop | true | Enables wraparound slide navigation. |
slideDistance | 10 | Distance between slides in pixels. |
slideAnimationDuration | 700 | Slide transition duration in milliseconds. |
heightAnimationDuration | 700 | Height transition duration in milliseconds. |
visibleSize | 'auto' | Extends the visible slider area to expose neighboring slides. |
centerSelectedSlide | true | Centers the selected slide when multiple slides are visible. |
rightToLeft | false | Uses right-to-left slide direction. |
Modules, Motion, and Navigation
| Option | Default | Description |
|---|---|---|
addOns | [] | Add-on classes to initialize with module imports. |
fade | false | Uses a fade transition between slides. |
fadeOutPreviousSlide | true | Fades out the previous slide during the fade transition. |
fadeDuration | 500 | Fade duration in milliseconds. |
autoplay | true | Enables automatic slide changes. |
autoplayDelay | 5000 | Interval between autoplay transitions in milliseconds. |
autoplayDirection | 'normal' | 'normal' or 'backwards'. |
autoplayOnHover | 'pause' | 'pause', 'stop', or 'none' on hover. |
arrows | false | Displays previous and next arrows. |
fadeArrows | true | Reveals slide arrows on hover. |
buttons | true | Displays pagination buttons. |
keyboard | true | Enables keyboard slide navigation. |
keyboardOnlyOnFocus | false | Restricts keyboard control to a focused slider. |
touchSwipe | true | Enables touch and mouse dragging of slides. |
touchSwipeThreshold | 50 | Minimum swipe distance in pixels. |
fadeCaption | true | Fades captions during slide changes. |
captionFadeDuration | 500 | Caption fade duration in milliseconds. |
fullscreen | false | Displays the fullscreen control. |
fadeFullscreen | true | Reveals the fullscreen control on hover. |
Layers, Images, Thumbnails, and Video
| Option | Default | Description |
|---|---|---|
waitForLayers | false | Waits for animated layers to hide before certain slide changes. |
autoScaleLayers | true | Scales layers as slides shrink. |
autoScaleReference | -1 | Reference width for layer scaling. -1 uses the configured slide width when possible. |
smallSize | 480 | Slider-width threshold for small images. |
mediumSize | 768 | Slider-width threshold for medium images. |
largeSize | 1024 | Slider-width threshold for large images. |
updateHash | false | Updates the location hash to identify the selected slide. |
reachVideoAction | 'none' | 'playVideo' or 'none' when selecting a video slide. |
leaveVideoAction | 'pauseVideo' | 'stopVideo', 'pauseVideo', 'removeVideo', or 'none' when leaving a video slide. |
playVideoAction | 'stopAutoplay' | 'stopAutoplay' or 'none' when video playback starts. |
pauseVideoAction | 'none' | 'startAutoplay' or 'none' when video playback pauses. |
endVideoAction | 'none' | 'startAutoplay', 'nextSlide', 'replayVideo', or 'none' when playback ends. |
thumbnailWidth | 100 | Thumbnail width in pixels. |
thumbnailHeight | 80 | Thumbnail height in pixels. |
thumbnailsPosition | 'bottom' | 'top', 'bottom', 'right', or 'left'. |
thumbnailPointer | false | Displays a selection pointer on the current thumbnail. |
thumbnailArrows | false | Shows thumbnail-strip arrows. |
fadeThumbnailArrows | true | Controls hover-based fading of thumbnail arrows. |
thumbnailTouchSwipe | true | Enables swiping the thumbnail strip. |
breakpoints | null | Maps browser-width thresholds to sets of slider options. |
Public Methods
| Method | Description |
|---|---|
gotoSlide(index) | Selects the slide at the zero-based index. |
nextSlide() | Selects the next slide. |
previousSlide() | Selects the previous slide. |
getSlideAt(index) | Returns the data object for a slide. |
getSelectedSlide() | Returns the current slide index. |
getTotalSlides() | Returns the slide count. |
update() | Refreshes the slider after markup changes. |
resize() | Recalculates slider dimensions. |
addEventListener(type, handler) | Registers a slider event handler. |
removeEventListener(type) | Removes the handler registered for that event identifier. |
destroy() | Removes slider behavior and restores its original markup state. |
Events
Register slider events with addEventListener() on the instance.
| Event | When it fires |
|---|---|
beforeInit | Before slider initialization. |
init | After initialization. |
beforeUpdate | Before a slider update. |
update | During the slider’s update sequence. |
beforeResize | Before recalculation of slider size. |
resize | After the slider is resized. |
gotoSlide | When a slide is selected. detail.index and detail.previousIndex identify the current and prior slides. |
gotoSlideComplete | After the slide transition completes. detail.index identifies the selected slide. |
HTML Classes and Data Attributes
Slide and Thumbnail Markup
| Class | Purpose |
|---|---|
.slider-pro | Root slider container. |
.sp-slides | Wrapper for the slide elements. |
.sp-slide | Individual slide. |
.sp-image | Main slide image. |
.sp-caption | Caption beneath a slide. |
.sp-thumbnails | Wrapper for an independent thumbnail strip. |
.sp-thumbnail | Thumbnail content, either in the strip or inside a slide. |
.sp-thumbnail-image | Image inside a composite thumbnail. |
.sp-thumbnail-text | Text inside a composite thumbnail. |
.sp-layer | Positioned or animated HTML layer inside a slide. |
.sp-video | Video content managed by the Video module. |
Layer Styling Classes
| Class | Purpose |
|---|---|
.sp-static | Keeps the layer visible rather than animating its entrance and exit. |
.sp-black | Dark translucent layer background with white text. |
.sp-white | Light translucent layer background with dark text. |
.sp-padding | Applies 10 pixels of padding. |
.sp-rounded | Rounds the layer corners. |
Layer Data Attributes
Use these attributes on elements with .sp-layer. data-position accepts topLeft, topCenter, topRight, bottomLeft, bottomCenter, bottomRight, centerLeft, centerRight, and centerCenter. The default is topLeft. Transition directions accept left, right, up, and down.
| Attribute | Purpose |
|---|---|
data-width | Layer width in pixels or a percentage. |
data-height | Layer height in pixels or a percentage. |
data-depth | Layer stacking order. |
data-position | Reference position within the slide. |
data-horizontal | Horizontal offset from the reference position. |
data-vertical | Vertical offset from the reference position. |
data-show-transition | Direction used when the layer appears. |
data-show-offset | Starting distance for the entrance animation. |
data-show-duration | Entrance animation duration. |
data-show-delay | Delay before the entrance animation. |
data-hide-transition | Direction used when the layer disappears. |
data-hide-offset | Ending distance for the exit animation. |
data-hide-duration | Exit animation duration. |
data-hide-delay | Delay before the exit animation. |
data-stay-duration | Time before the layer hides automatically. |
Image Source Attributes
| Attribute | Purpose |
|---|---|
data-src | Source image used by the lazy-loading module. |
data-retina | High-density display image. |
data-small | Alternative image for small slider widths. |
data-medium | Alternative image for medium slider widths. |
data-large | Alternative image for large slider widths. |
data-retinasmall | High-density version of the small image. |
data-retinamedium | High-density version of the medium image. |
data-retinalarge | High-density version of the large image. |
CSS Module Imports
The npm package exports its complete stylesheet through slider-pro-js/css. The following public imports are available when a bundler can process CSS imports. Only import the module styles your project uses, along with the core stylesheet.
| Import | Styles |
|---|---|
slider-pro-js/css | All bundled styles. |
slider-pro-js/css/core | Slider structure and base slides. |
slider-pro-js/css/arrows | Previous and next controls. |
slider-pro-js/css/buttons | Pagination controls. |
slider-pro-js/css/caption | Slide captions. |
slider-pro-js/css/fullscreen | Fullscreen control. |
slider-pro-js/css/layers | Layer styles and presets. |
slider-pro-js/css/thumbnails | Thumbnail containers and items. |
slider-pro-js/css/thumbnail-arrows | Thumbnail-strip arrows. |
slider-pro-js/css/thumbnail-touch-swipe | Thumbnail swipe styling. |
slider-pro-js/css/touch-swipe | Main slider drag styling. |
slider-pro-js/css/video | Video slide styling. |
Available Modules
The npm entry point exports the following add-on classes. The compiled browser build includes all of them. Pass the desired class constructors in addOns when importing the JavaScript as modules.
| Add-on | Purpose |
|---|---|
Arrows | Previous and next controls. |
Autoplay | Automatic slide progression. |
Breakpoints | Width-dependent settings. |
Buttons | Pagination buttons. |
Caption | Slide captions. |
ConditionalImages | Alternative images at size thresholds. |
DeepLinking | URL-hash navigation. |
Fade | Fade transitions. |
Fullscreen | Fullscreen button and mode. |
Keyboard | Keyboard navigation. |
Layers | Positioned or animated slide content. |
LazyLoading | Deferred image loading. |
Retina | High-density image selection. |
Thumbnails | Thumbnail navigation. |
ThumbnailArrows | Thumbnail-strip arrows. |
ThumbnailTouchSwipe | Swipe navigation in the thumbnail strip. |
TouchSwipe | Swipe and drag navigation in the main slider. |
Video | Playback handling for video slides. |
Alternatives & Related Resources
- Create Modern Mobile-friendly Carousels With Slickless.js
- Modern Image Carousel with Thumbnail Transitions
- Vanilla JS Carousel Slider With Modern Look and Auto-Play
- Create A Responsive Keyboard-Accessible Image Carousel with JavaScript







