0.1.4 • Published 7 years ago

dc-rangeslide v0.1.4

Weekly downloads
18
License
MIT
Repository
github
Last release
7 years ago

rangeslide.js

Customizable range slider with labels (but without jQuery)

Table of contents

Screenshots

Rangeslide with labels and tooltip Example with different label positioning

Quick start

Several quick start options are available:

Download the latest build

Development
Production

Install From Bower

bower install rangeslide.js --save

Install From Npm

npm install rangeslide.js --save

Done!

Usage

Including files:

<link rel="stylesheet" href="/path/to/rangeslide.css">
<script src="/path/to/rangeslide.js"></script>

Required HTML structure

<div id="rangeslide" class="rangeslide"></div>

Initialization

All you need to do is invoke rangeslide on an element:

var myRangeslide = rangeslide(domNode, options);

You can also initialize with css selector string:

var myRangeslide = rangeslide("#elementId", options);

Examples

There are some example usages that you can look at to get started. They can be found in the examples folder.

Options

rangeslide.js can accept an options object to alter the way it looks and behaves. If no options object is passed default values are used. The example of a structure of an options object is as follows:

{
  data: [
  	{ name: "example1", item: "Example text" },
  	{ name: "example2", item: "Another example text" },
  	{ name: "example3", item: function() { return "And yet another example text"; }
  ],
  showLabels: true,
  showTicks: true,
  labelsPosition: "alternate",
  startAlternateLabelsFromTop: true,
  startPosition: 0,
  thumbHeight: 16,
  thumbWidth: 16,
  handlers: {
    initialized: function(rangeslideElement) {},
    valueChanged: function(data, rangeslideElement) {},
    markerClicked: function(data, markerElement) {}
  }

Here is the explanation of options object:

OptionTypeDescriptionDefaultOptions
autoPlayBooleanEnable auto play modefalse
autoPlayDelayNumberDelay (in miliseconds) between steps in auto play mode1000
dataArrayArray of data objects (property 'name' is mandatory[]
enableLabelClickBooleanEnable click on labelstrue
enableMarkerClickBooleanEnable click on track markerstrue
enableTrackClickBooleanEnable click on tracktrue
handlersObjectEvent handlers collection{}
highlightSelectedLabelsBooleanEnable highlighting of selected labelsfalse
labelsPositionStringPosition of labels on a rangeslide'below''above', 'below', 'alternate'
labelsWidthNumberWidth of labels60
leftLabelStringText to display on left side label""
loopBooleanEnable loop when auto play mode is activetrue
markerSizeNumberSize of track marker (pixels)14
rightLabelStringText to display on right side label""
showLabelsBooleanShow/hide labelsfalse
sideLabelsWidthNumberWidth of left and right side labels40
showTrackMarkersProgressBooleanIndicate progress on markersfalse
showTicksBooleanShow/hide label ticksfalse
showTrackMarkersBooleanShow/hide track markersfalse
showTrackProgressBooleanIndicate progress on trackfalse
showValueBooleanShow/hide value indicatorfalse
startAlternateLabelsFromTopBooleanShow first label above if labelsPosition is 'alternate'false
startPositionNumberInitial position of rangeslide thumb0
stepSizeNumberSize of rangeslider step16
thumbHeightNumberHeight of thumb element (in pixels)16
thumbWidthNumberWidth of thumb element (in pixels)16
tickHeightNumberHeight of label's tick (in pixels)16
trackHeightNumberHeight of rangeslide track (in pixels)7
valueIndicatorOffsetNumberValue indicator offset from rangeslider thumb5
valueIndicatorWidthNumberWidth of value indicator (in pixels)32
valueIndicatorHeightNumberHeight of value indicator (in pixels)32
valuePositionStringPosition of value indicator'thumb''above', 'below', 'thumb'
valueSourceStringSource of data for value indicator'index''index', 'name', 'item'

Data

Data collection for rangeslide is provided during instatiation or later using "data" attribute in options. Number of steps (markers) for a slider is equal to number of items passed in this attribute. Each data item is required to have at least "name" attribute (String). You can also pass additional data using "item" attribute. Below is an example of proper data setup:

rangeslide("#element" {
	data: [
    	{ name: "data1", item: "simple string data"},
        { name: "data2", item: ["data", "in", "a", "form", "of ", "an", "array"] },
        { name: "data3", item: function() { return "data in a form of a function"; } },
        { name: "data4", item: { "passing": "object", "as": "data" } },
        { name: "data5" }
    ]
})

Rangeslide constructed with the above data will have 3 steps (markers).

Methods

Methods are called on rangeslide instances. You shoud save the instances to variable to have further access to it.

Object getValue()

Get current rangeslide value.

var item = rangeslide.getValue();

DOMNode getElement()

Gets rangeslide's DOM node object.

var node = rangeslide.getElement();

void setValue(Number index)

Set rangeslide value by specifying data index.

rangeslide.setValue(2);

void setValueByName(String name)

Set rangeslide value by specifying data name. If multiple data items with the same names are found first one is set.

rangeslide.setValueByName("exampleName");

void setOption(String name, Object value)

Gets rangeslide's DOM node object.

rangeslide.setOption("autoPlay", true);

void refresh()

Manually refresh and redraw rangeslide. Invoked automatically when options change.

rangeslide.refresh();

Events

rangeslide.js provides custom events for some of it's actions. Appropriate callbacks can be specified in options.

EventDescriptionArguments
valueChangedFires after slider value is changeddataItem, rangeslideElement
playStartFires when auto play is starteddataItem, rangeslideElement
playStopFires when auto play is stoppeddataItem, rangeslideElement
initializedFires after slider is initializedrangeslideElement
refreshedFires after slider is refreshedrangeslideElement
trackClickedFires after slider track is clickeddataItem, trackElement
labelClickedFires after label is clickeddataItem, labelElement
markerClickedFires after track marker is clickeddataItem, markerElement
thumbDragStartFires when thumb dragging is initiateddataItem, thumbElement
thumbDraggedFires when thumb is being draggeddataItem, thumbElement
thumbDraggedEndFires when thumb is droppeddataItem, thumbElement

Event handlers are passed in rangeslide options like in the example below:

rangeslide("#element" {
	handlers: {
    	"valueChanged": [function(data, element) { console.log(data); }],
        "labelClicked": [
        	function(data, element) { console.log(data); },
            function(data, element) { return data.name; },
            function(data, element) { element.style.backroundColor = "#e2e3e4"; },
        ]
    }
})

Copyright and license

Licensed under MIT license.

^ back to top