Tiny Slider
Vanilla javascript slider for all purposes.
README
Tiny slider for all purposes, inspired by Owl Carousel.
<!-- Tests for desktop browsers (running on Firefox 12+, Chrome 15+, Safari 5.1+, Opera 12.1+, IE9+)
Tests for mobile browsers (running on Android Browser 4.2+)
-->
Previous versions:
v1,
Warning: tiny-slider works with static content and it works in the browser only. If the HTML is loaded dynamically, make sure to call tns() after its loading.
Contents
+ Features
+ Install
+ Usage
+ Options
+ Methods
+ Fallback
+ Support
+ License
What's new
- Using % instead of px (No more recalculation of each slide width on window resize)
- Using CSS Mediaqueries if supported
- Save browser capability values to localStorage, so they will not be recheck again until browser get upgraded or user clear the localStorage manually.
- Some selector changes in [tiny-slider.scss](https://github.com/ganlanyuan/tiny-slider/blob/master/src/tiny-slider.scss)
Migrating to v2
- Update controls and / or nav styles based on their position changes.
- Update the [slider selectors](https://github.com/ganlanyuan/tiny-slider/blob/master/src/tiny-slider.scss) accordingly if used in your CSS or JS.
- Update styles related to autoplay button.
Features
Carousel * | Gallery | ||||
---|---|---|---|---|---|
Horizontal * | Vertical | ||||
Percentage Width * | Fixed Width | Auto Width | |||
Loop | ✓ | ✓ | ✓ | ✓ | ✓ |
Rewind | ✓ | ✓ | ✓ | ✓ | |
Slide by | ✓ | ✓ | ✓ | ✓ | |
Gutter | ✓ | ✓ | ✓ | ✓ | ✓ |
Edge padding | ✓ | ✓ | ✓ | ✓ | |
Center (v2.9.2+) | ✓ | ✓ | ✓ | ||
Responsive | ✓ | ✓ | ✓ | ✓ | ✓ |
Lazyload | ✓ | ✓ | ✓ | ✓ | ✓ |
Autoplay | ✓ | ✓ | ✓ | ✓ | ✓ |
Auto height | ✓ | ✓ | ✓ | ✓ | ✓ |
Touch/drag | ✓ | ✓ | ✓ | ✓ | ✓ |
Arrow keys | ✓ | ✓ | ✓ | ✓ | ✓ |
Customize controls/nav | ✓ | ✓ | ✓ | ✓ | ✓ |
Accessibility | ✓ | ✓ | ✓ | ✓ | ✓ |
Respond to DOM visibility changes | ✓ | ✓ | ✓ | ✓ | ✓ |
Custom events | ✓ | ✓ | ✓ | ✓ | ✓ |
Nested | ✓ | ✓ | ✓ | ✓ | ✓ |
Install
bower install tiny-slider or npm install tiny-slider
Usage
1. Add CSS (and IE8 polyfills if needed)
- ``` html
- <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/tiny-slider/2.9.4/tiny-slider.css">
- !--[if (lt IE 9)]><script src="https://cdnjs.cloudflare.com/ajax/libs/tiny-slider/2.9.4/min/tiny-slider.helper.ie8.js"></script>![endif]-->
- ```
2. Add markup
- ``` html
- <div class="my-slider">
- <div></div>
- <div></div>
- <div></div>
- </div>
- ```
3. Call tns()
Option A: Add tiny-slider.js to your page:
- ``` html
- <script src="https://cdnjs.cloudflare.com/ajax/libs/tiny-slider/2.9.2/min/tiny-slider.js"></script>
- ```
Option B: Import tns via webpack or rollup:
- ``` js
- // yourScript.js
- import { tns } from "./node_modules/tiny-slider/src/tiny-slider"
- ```
Option C: Import tns directly start from v2.8.2
- ``` html
- <script type="module">
- import {tns} from './src/tiny-slider.js';
- var slider = tns({
- container: '.my-slider',
- items: 3,
- slideBy: 'page',
- autoplay: true
- });
- </script>
- ```
Options
Option | Type | Description |
---|---|---|
--- | --- | --- |
`container` | Node | String |
`mode` | "carousel" | "gallery" |
`axis` | "horizontal" | "vertical" |
`items` | positive | Default: |
`gutter` | positive | Default: |
`edgePadding` | positive | Default: |
`fixedWidth` | positive | false |
`autoWidth` | Boolean | Default: |
`viewportMax` | positive | false |
`slideBy` | positive | "page" |
`center` | Boolean | Default: |
`controls` | Boolean | Default: |
`controlsPosition` | "top" | "bottom" |
`controlsText` | (Text | Markup) |
`controlsContainer` | Node | String |
`prevButton` | Node | String |
`nextButton` | Node | String |
`nav` | Boolean | Default: |
`navPosition` | "top" | "bottom" |
`navContainer` | Node | String |
`navAsThumbnails` | Boolean | Default: |
`arrowKeys` | Boolean | Default: |
`speed` | positive | Default: |
`autoplay` | Boolean | Default: |
`autoplayPosition` | "top" | "bottom" |
`autoplayTimeout` | positive | Default: |
`autoplayDirection` | "forward" | "backward" |
`autoplayText` | Array | Markup) |
`autoplayHoverPause` | Boolean | Default: |
`autoplayButton` | Node | String |
`autoplayButtonOutput` | Boolean | Default: |
`autoplayResetOnVisibility` | Boolean | Default: |
`animateIn` | String | Default: |
`animateOut` | String | Default: |
`animateNormal` | String | Default: |
`animateDelay` | positive | false |
`loop` | Boolean | Default: |
`rewind` | Boolean | Default: |
`autoHeight` | Boolean | Default: |
`responsive` | Object: | false |
`lazyload` | Boolean | Default: |
`lazyloadSelector` | String | Default: |
`touch` | Boolean | Default: |
`mouseDrag` | Boolean | Default: |
`swipeAngle` | positive | Boolean |
`preventActionWhenRunning` | Boolean | Default: |
`preventScrollOnTouch` | "auto" | "force" |
`nested` | "inner" | "outer" |
`freezable` | Boolean | Default: |
`disable` | Boolean | Default: |
`startIndex` | positive | Default: |
`onInit` | Function | false |
`useLocalStorage` | Boolean | Default: |
`nonce`| | Default: |
NOTE:
Prior to v2.0.2, options "container", "controlsContainer", "navContainer" and "autoplayButton" still need to be DOM elements.
E.g. container: document.querySelector('.my-slider')
Responsive options
The following options can be redefined in responsive field:
startIndex,
items,
slideBy,
speed,
autoHeight,
fixedWidth,
edgePadding,
gutter,
center,
controls,
controlsText,
nav,
autoplay,
autoplayHoverPause,
autoplayResetOnVisibility,
autoplayText,
autoplayTimeout,
touch,
mouseDrag,
arrowKeys,
disable
- ``` js
- <script>
- var slider = tns({
- container: '.my-slider',
- items: 1,
- responsive: {
- 640: {
- edgePadding: 20,
- gutter: 20,
- items: 2
- },
- 700: {
- gutter: 30
- },
- 900: {
- items: 3
- }
- }
- });
- </script>
- ```
NOTE:
+ The breakpoints behave like (min-width: breakpoint) in CSS, so an undefined option will be inherited from previous small breakpoints.
+ fixedWidth can only be changed to other positive integers. It can't be changed to negative integer, 0 or other data type.
Methods
The slider returns a slider object with some properties and methods once it's initialized:
- ``` js
- {
- version: version, // tiny-slider version
- getInfo: info(),
- events: events, // Object
- goTo: goTo(),
- play: play(),
- pause: pause(),
- isOn: isOn, // Boolean
- updateSliderHeight: updateInnerWrapperHeight(),
- refresh: initSliderTransform(),
- destroy: destroy(),
- rebuild: rebuild()
- }
- ```
To get the slider information, you can either use the getInfo() method or subscribe to an Event. Both return an Object:
- ``` js
- {
- container: container, // slider container
- slideItems: slideItems, // slides list
- navContainer: navContainer, // nav container
- navItems: navItems, // dots list
- controlsContainer: controlsContainer, // controls container
- hasControls: hasControls, // indicate if controls exist
- prevButton: prevButton, // previous button
- nextButton: nextButton, // next button
- items: items, // items on a page
- slideBy: slideBy // items slide by
- cloneCount: cloneCount, // cloned slide count
- slideCount: slideCount, // original slide count
- slideCountNew: slideCountNew, // total slide count after initialization
- index: index, // current index
- indexCached: indexCached, // previous index
- displayIndex: getCurrentSlide(), // display index starts from 1
- navCurrent: navCurrent, // current dot index
- navCurrentCached: navCurrentCached, // previous dot index
- pages: pages, // visible nav indexes
- pagesCached: pagesCached,
- sheet: sheet,
- event: e || {}, // event object if available
- };
- ```
getInfo
Get slider information.
- ``` js
- slider.getInfo();
- document.querySelector('.next-button').onclick = function () {
- // get slider info
- var info = slider.getInfo(),
- indexPrev = info.indexCached,
- indexCurrent = info.index;
- // update style based on index
- info.slideItems[indexPrev].classList.remove('active');
- info.slideItems[indexCurrent].classList.add('active');
- };
- ```
goTo
Go to specific slide by number or keywords.
- ``` js
- slider.goTo(3);
- slider.goTo('prev');
- slider.goTo('next');
- slider.goTo('first');
- slider.goTo('last');
- document.querySelector('.goto-button').onclick = function () {
- slider.goTo(3);
- };
- ```
play
Programmatically start slider autoplay when autoplay: true.
- ``` js
- slider.play();
- ```
pause
Programmatically stop slider autoplay when autoplay: true.
- ``` js
- slider.pause();
- ```
updateSliderHeight
Manually adjust slider height when autoHeight is true.
- ``` js
- slider.updateSliderHeight();
- ```
destroy
Destroy the slider.
- ``` js
- slider.destroy();
- ```
rebuild
Rebuild the slider after destroy.
- ``` js
- slider = slider.rebuild();
- // this method returns a new slider Object with the same options with the original slider
- ```
Custom Events
Available events include: indexChanged, transitionStart, transitionEnd, newBreakpointStart, newBreakpointEnd, touchStart, touchMove, touchEnd, dragStart, dragMove and dragEnd.
- ``` js
- var customizedFunction = function (info, eventName) {
- // direct access to info object
- console.log(info.event.type, info.container.id);
- }
- // bind function to event
- slider.events.on('transitionEnd', customizedFunction);
- // remove function binding
- slider.events.off('transitionEnd', customizedFunction);
- ```
Fallback
- ```css
- .no-js .your-slider { overflow-x: auto; }
- .no-js .your-slider > div { float: none; }
- ```
Browser Support
Desktop:
Firefox 8+ ✓
Chrome 15+ ✓ (Should works on _Chrome 4-14_ as well, but I couldn't test it.)
Safari 4+ ✓
Opera 12.1+ ✓
IE 8+ ✓
Mobile:
Android Browser 4.2+ ✓
Chrome Mobile 63+ ✓
Firefox Mobile 28+ ✓
Maxthon 4+ ✓
Support
Live tests and Automated Tests
Live tests, Screenshots and Automated Tests
Images on demo page are from https://unsplash.com/.
License
This project is available under the MIT license.