• Stars
    star
    601
  • Rank 74,537 (Top 2 %)
  • Language
    JavaScript
  • License
    MIT License
  • Created over 11 years ago
  • Updated over 1 year ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

🎠 The 1kb JavaScript Carousel

simple-slider

NPM version CDNJS version Build Status File Size: < 1.1kB gzipped license Join the chat at https://gitter.im/simple-slider/Lobby

https://ruyadorno.github.io/simple-slider

Extremely lightweight carousel micro library.

Build Status

About

simple-slider is a carousel micro library based on the requestAnimationFrame API. It makes for a highly testable implementation and less css-dependent.

This package contains a framework agnostic implementation. If you are using AngularJS or Polymer there are some simple-slider framework-specific implementations available:

Features

  • Small size, less than 1.1kb minified/gzipped
  • Support to UMD: AMD, CommonJS and global definition
  • Uses requestAnimationFrame for animations
  • Supports Page visibility API to pause/resume transitions when user navigates away from the page
  • Accept ease functions to customize the transition animation
  • Lots of ready-to-use examples available, just check the example folder
  • Animates any numerical css property

Install

Available on npm:

npm install --save simple-slider

and you can also get it on Bower:

bower install --save simple-slider

Getting it from cdnjs

This library is also available on https://cdnjs.com/ you can use it just by importing:

Usage

Simply import the script in your html and call the simpleslider.getSlider function.

<div style="width:612px; height:612px" data-simple-slider>
  <img src="https://picsum.photos/612/612?random=1"/>
  <img src="https://picsum.photos/612/612?random=2"/>
</div>
<script src="https://cdnjs.cloudflare.com/ajax/libs/simple-slider/1.0.0/simpleslider.min.js"></script>
<script>
  simpleslider.getSlider();
</script>

In this previous example we didn't specified any additional option, in this case the slider will use its default left-to-right sliding animation and the container element will be the first element containing a data-simple-slider attribute.

Usage in a CommonJS environment

var simpleslider = require('simple-slider');

simpleslider.getSlider();

ES2015+ environments

import { getSlider } from 'simple-slider/src';

getSlider();

RequireJS/AMD environment

require(['simple-slider'], function(simpleslider) {
  simpleslider.getSlider();
});

Options

Options are set as named properties of a single parameter accepted by the getSlider function, they help you customize the slider transition and how it's going to work.

The main option is a container element, this will usually be a <div> or <ul> containing the elements to be transitioned, keep in mind that this container should also have a defined width/height value. You can also tweak things such as the delay time between each transition, how long each transition will take, etc.

<div id="myslider" style="width:612px; height:612px">
  <img src="https://picsum.photos/612/612?random=1"/>
  <img src="https://picsum.photos/612/612?random=2"/>
</div>
<script src="https://cdnjs.cloudflare.com/ajax/libs/simple-slider/1.0.0/simpleslider.min.js"></script>
<script>
  simpleslider.getSlider({
    container: document.getElementById('myslider'),
    transitionTime:1,
    delay:3.5
  });
</script>

Available Options

Here is the list of available values to customize how your slider is going to work:

  • container: <Element> The HTML element that act as a container for the slider. Defaults to document.querySelector('*[data-simple-slider]).
  • children <NodeList/Array> A list of children to be used as slides, you can use the querySelectorAll to have more flexibility on what children of the container element should be used as slides. Defaults to container.children.
  • paused: Controls carousel auto-transition/slideshow. If value is true no transition will happen. Defaults to false.
  • prop: Determines the css property to be animated. Defaults to left.
  • duration: Value setting the duration of animation transition. Defaults to 0.5.
  • delay: Value determining the wait between each animation when auto-transition is enabled. Defaults to 3 seconds.
  • init: <String/Number> Initial value of slide elements when starting a transition animation. Defaults to -100.
  • show: <String/Number> The value a slide element should have when it is displayed. Defaults to 0.
  • end: <String/Number> The value a slide will move to during a transition animation. Defaults to 100.
  • unit: The css unit value to be used. Defaults to %.
  • ease: An ease function, you can use any of these. Defaults to defaultEase internal function.
  • onChange: A callback function to be invoked each time a slide changes.
  • onChangeEnd: A callback function to be invoked at the end of the slide transition

Default values

{
  container: document.querySelector('*[data-simple-slider]'),
  children: container.children,
  paused: false,
  prop: 'left',
  duration: 0.5,
  delay: 3,
  init: -100,
  show: 0,
  end: 100,
  unit: '%',
  ease: defaultEase function,
  onChange: undefined,
  onChangeEnd: undefined
}

Programmatic API

Some methods are exposed by the returning value of the function allowing you to control the slider.

<div id="myslider" style="width:612px; height:612px">
  <img src="http://placekitten.com/g/612/612"/>
  <img src="http://placekitten.com/g/612/613"/>
</div>
<script src="../dist/simpleslider.min.js"></script>
<script>
  var currentIndex;

  function updateCurrentIndex() {
    currentIndex = slider.currentIndex();
  }

  var slider = simpleslider.getSlider({
    container: document.getElementById('myslider'),
    onChangeEnd: updateCurrentIndex
  });

  // pauses slideshow
  slider.pause();
</script>

Available methods:

  • currentIndex() Returns the index of the current displaying image.
  • pause() Pauses the slideshow.
  • resume() Resumes the slideshow.
  • reverse() Swaps init for end and reverses the order of slides.
  • nextIndex() Gets the index of the next slide to be shown.
  • prevIndex() Gets the index of the previous slide.
  • next() Switches displaying image to the next one.
  • prev() Switches displaying image to the previous one.
  • change(index) Changes image to a given index value.
  • dispose() Disposes of all internal variable references.

More examples

Documentation

Extensive documentation of the options and methods can be found at the simple-slider official documentation.

Alternatives

A JavaScript carousel micro library is not a new thing (fun fact, simple-slider has been around since 2013).

I would recommend that you take a look at some of the available alternatives and decide by yourself which one better suits your needs.

License

MIT © Ruy Adorno

More Repositories

1

ntl

Node Task List: Interactive cli to list and run package.json scripts
JavaScript
888
star
2

ipt

Interactive Pipe To: The Node.js cli interactive workflow
JavaScript
843
star
3

snapstub

Copy API endpoints to your fs and run a local server using them
JavaScript
255
star
4

dom-i18n

Provides a very basic HTML multilingual support using JavaScript
JavaScript
139
star
5

grunt-menu

Useful menu interface for listing/executing your configured tasks
JavaScript
79
star
6

installme-osx

My personal script to setup a new OSX
Shell
60
star
7

git-bc

Git plugin to interactively list branches and checkout
JavaScript
60
star
8

generator-umd

An Yeoman Generator to create a basic UMD structure
JavaScript
28
star
9

console-faker

Get some file content to be printed out on console while you type anything on your keyboard.
JavaScript
26
star
10

preact-jest-snapshot-test-boilerplate

🚀 Test Preact components using Jest snapshots
JavaScript
23
star
11

mitro-cli

Manage your Mitro passwords from a command line interface.
JavaScript
22
star
12

telegram-pi-bot

A python-telegram-bot setup to run on Raspberry pi
Python
22
star
13

polymer-simple-slider

A Polymer element providing a simple slider functionality
HTML
22
star
14

jest-serializer-html-string

A better Jest snapshot serializer for plain html strings
JavaScript
17
star
15

prettierme

Fastest way to run prettier for a single file. Fork of https://github.com/ruyadorno/eslintme
Shell
15
star
16

angular-simple-slider

An AngularJS directive providing a simple slider functionality
JavaScript
15
star
17

checkbower

Validates your bower.json files
JavaScript
13
star
18

itrash

Interactively selects files to delete from current folder in the cli
JavaScript
13
star
19

git-iadd

Git plugin that allows you to interactively select which files to add to stage (git add)
JavaScript
12
star
20

dotfiles

Shell
11
star
21

mail-sender

A simple python script to send an html email using smtplib
Python
10
star
22

clean-dir

Command line util to clean a directory
JavaScript
9
star
23

request-hash

Generate a hash from an express Request or http.IncomingMessage
JavaScript
8
star
24

path-complete-extname

path.extname implementation adapted to also include multiple dots extensions.
JavaScript
8
star
25

eslintme

The fastest way to eslint a single file
Shell
7
star
26

extract-path

Extract a valid file system path from a string
JavaScript
7
star
27

lru-cache-fs

Extends lru-cache to add file system support
JavaScript
6
star
28

estimate

Application to help time estimation for digital agencies.
Python
5
star
29

js-signals-factory

A small factory implementation for js-signals.
JavaScript
5
star
30

git-pr

Interactive checkout PRs using the GitHub cli
Shell
4
star
31

git-ishow

Git plugin that lets you interactively select a stashed item to show
JavaScript
4
star
32

tlr

Quick spawn a tiny-lr server
JavaScript
4
star
33

nextjs-api-fetch-example

A simple example using fetched API data to render components
JavaScript
4
star
34

simple-output

Output messages to stdout/stderr
JavaScript
4
star
35

extend-grunt-plugin

Helper method to easily extend an already existing grunt task.
JavaScript
4
star
36

create-100-workshop

The road to 100% test coverage workshop
JavaScript
3
star
37

npx-utils

👾 Manages packages in the npx-space
JavaScript
3
star
38

simple-data-structures

🌅 Basic data structures with tons of tests and helper functions
Python
2
star
39

cli-all-the-things

Talk at ForwardJS 2020
JavaScript
2
star
40

npm-community-landing-page

HTML
2
star
41

outdated

Shortcut to interactively update an outdated dep using npm + ipt
Shell
1
star
42

redact

Small cli util that redacts path refs from stdin
JavaScript
1
star
43

stubborn-server-hash-loader

Loader plugin for stubborn-server
JavaScript
1
star
44

ruyadorno.github.io

HTML
1
star
45

mitro-js

Mirror of the API to access Mitro server side from node applications.
JavaScript
1
star
46

vim-change-indent

Vim plugin to convert the indent sizes in a file.
Vim Script
1
star
47

ruyadorno

1
star
48

generator-jsmontreal

A test generator to be presented at js-montreal 02/10 meetup
JavaScript
1
star
49

checkgituser

Checks a folder to find .git/config files without an [user] field declared
Python
1
star
50

the-one-with-test-coverage

Demo repo for my test coverage talk from JS Montreal 10/2019
JavaScript
1
star