Glider.js

A fast, lightweight, dependency free, native scrolling carousel alternative...

README

Glider.js


Latest Version: 1.7.8
A fast, light-weight, dependency free, responsive, accessible, extendable, native scrolling list with paging controls, methods and events. (< 2.8kb gzipped!)

Demos and full documentation available on Github Pages: https://nickpiscitelli.github.io/Glider.js/

Quick Start

Include glider.min.css:

  1. ``` html
  2. <link rel="stylesheet" href="glider.min.css">
  3. or
  4. <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/glider-js@1/glider.min.css">
  5. ```

Include Glider.js:

  1. ``` html
  2. <script src="glider.min.js"></script>
  3. or
  4. <script src="https://cdn.jsdelivr.net/npm/glider-js@1/glider.min.js"></script>
  5. ```

Example HTML:

  1. ``` html
  2. <div class="glider">
  3.   <div> 1 </div>
  4.   <div> 2 </div>
  5.   <div> 3 </div>
  6.   <div> 4 </div>
  7.   <div> 5 </div>
  8.   <div> 6 </div>
  9. </div>
  10. ```

Glider.js Initialization

  1. ``` js
  2. new Glider(document.querySelector('.glider'));
  3. ```

Glider.js Initialization w/ full options:

  1. ``` js
  2. new Glider(document.querySelector('.glider'), {

  3.   // `auto` allows automatic responsive
  4.   // width calculations
  5.   slidesToShow: 'auto',
  6.   slidesToScroll: 'auto',

  7.   // should have been named `itemMinWidth`
  8.   // slides grow to fit the container viewport
  9.   // ignored unless `slidesToShow` is set to `auto`
  10.   itemWidth: undefined,

  11.   // if true, slides wont be resized to fit viewport
  12.   // requires `itemWidth` to be set
  13.   // * this may cause fractional slides
  14.   exactWidth: false,

  15.   // speed aggravator - higher is slower
  16.   duration: .5,

  17.   // dot container element or selector
  18.   dots: 'CSS Selector',

  19.   // arrow container elements or selector
  20.   arrows: {
  21.     prev: 'CSS Selector',
  22.     // may also pass element directly
  23.     next: document.querySelector('CSS Selector')
  24.   },

  25.   // allow mouse dragging
  26.   draggable: false,
  27.   // how much to scroll with each mouse delta
  28.   dragVelocity: 3.3,

  29.   // use any custom easing function
  30.   // compatible with most easing plugins
  31.   easing: function (x, t, b, c, d) {
  32.     return c*(t/=d)*t + b;
  33.   },

  34.   // event control
  35.   scrollPropagate: false,
  36.   eventPropagate: true,

  37.   // Force centering slide after scroll event
  38.   scrollLock: false,
  39.   // how long to wait after scroll event before locking
  40.   // if too low, it might interrupt normal scrolling
  41.   scrollLockDelay: 150,

  42.   // Force centering slide after resize event
  43.   resizeLock: true,

  44.   // Glider.js breakpoints are mobile-first
  45.   responsive: [
  46.     {
  47.       breakpoint: 900,
  48.       settings: {
  49.         slidesToShow: 2,
  50.         slidesToScroll: 2
  51.       }
  52.     },
  53.     {
  54.       breakpoint: 575,
  55.       settings: {
  56.         slidesToShow: 3,
  57.         slidesToScroll: 3
  58.       }
  59.     }
  60.   ]
  61. });
  62. ```

Change options:

  1. ``` js
  2. Glider(document.querySelector(element_path)).setOption({
  3.   name: value,
  4.   ...
  5. });

  6. // optionally call refresh
  7. Glider(document.querySelector(element_path)).refresh();
  8. ```

Bind event:

  1. ``` js
  2. document.querySelector(element_path).addEventListener('glider-slide-visible', function(event){
  3.   // `this` is bound to the glider element
  4.   // custom data located at `event.detail`
  5.   // access to Glider object via `Glider(this)`
  6.   ...
  7. });
  8. ```

Destroy with:

  1. ``` js
  2. Glider(document.querySelector(element_path)).destroy();
  3. ```

Install using package managers NPM / YARN


  1. ```
  2. $ npm install glider-js
  3. ```

  1. ```
  2. $ yarn add glider-js
  3. ```

Browser support


Glider.js should run on all modern browsers. Support for older browser can be achieved by polyfilling document.classList, window.requestAnimationFrame, Object.assign and CustomEvent

Include glider-compat.min.js to load the aforementioned polyfills

Native Scrollbars


Most browsers now support the scrollbar-width property allowing us to avoid the messy hack below.

NOTE: This feature is marked as experimental and may not work in all browsers.

  1. ```
  2. .glider-track {
  3.   scrollbar-width: none;
  4. }
  5. ```

Since Glider.js uses native scrolling, the browser wants to apply the standard scrollbar to the glider. In most cases, this is fine since the scrollbar can be hidden with CSS and Glider.js does so when appropriate. In browsers such as Firefox though, the scrollbars cannot be hidden with CSS and require additional markup to hide.

To hide the scrollbars in Firefox, you'll want to wrap your glider with `
` and apply the following CSS/JS:

  1. ```css
  2. @-moz-document url-prefix() {
  3.   .glider-track {
  4.     margin-bottom: 17px;
  5.   }
  6.   .glider-wrap {
  7.     overflow: hidden;
  8.   }
  9. }
  10. ```

  1. ``` js
  2. document.addEventListener('glider-loaded', hideFFScrollBars);
  3. document.addEventListener('glider-refresh', hideFFScrollBars);
  4. function hideFFScrollBars(e){
  5.   var scrollbarHeight = 17; // Currently 17, may change with updates
  6.   if(/firefox/i.test(navigator.userAgent)){
  7.     // We only need to appy to desktop. Firefox for mobile uses
  8.     // a different rendering engine (WebKit)
  9.     if (window.innerWidth > 575){
  10.       e.target.parentNode.style.height = (e.target.offsetHeight - scrollbarHeight) + 'px'
  11.     }
  12.   }
  13. }
  14. ```

Packages using Glider.js :rocket:


- react-glider - A react wrapper for Glider.js written in typescript.

Dependencies


None :)

License


Copyright (c) 2018 Nick Piscitelli

Licensed under the MIT license.

It's all yours.