Range Slider - 图1

Range Slider

Range Slider Layout

Single Range

Single range slider layout is pretty simple:

  1. <!-- Range Slider element -->
  2. <div class="range-slider">
  3. <!-- range input -->
  4. <input type="range" min="0" max="100" step="1" value="10">
  5. </div>

Dual Range

Dual range slider is more simpler as it doesn’t require input element because input:range doesn’t support dual range:

  1. <!-- Range Slider element -->
  2. <div class="range-slider"></div>

Range Slider Colors

Range Slider supports all default colors. So to change its color just add color-[color] class to range slider element.

  1. <!-- red range -->
  2. <div class="range-slider color-red">...</div>
  3. <!-- orange range -->
  4. <div class="range-slider color-orange">...</div>

Range Slider App Methods

Let’s look at related App methods to work with Range Slider:

app.range.create(parameters)- create Range Slider instance

  • parameters - object. Object with range slider parameters

Method returns created Range Slider’s instance

app.range.destroy(el)- destroy Range Slider instance

  • el - HTMLElement or string (with CSS Selector) or object. Range Slider element or Range Slider instance to destroy.

app.range.get(el)- get Range Slider instance by HTML element

  • el - HTMLElement or string (with CSS Selector). Range Slider element.

Method returns Range Slider’s instance

app.range.getValue(el)- get Range Slider value

  • el - HTMLElement or string (with CSS Selector). Range Slider element.

Method returns range slider value

app.range.setValue(el, value)- set new Range Slider value

  • el - HTMLElement or string (with CSS Selector). Range Slider element.
  • value - number (in case of single range) or array of values (in case of dual range)

Method returns Range Slider’s instance

Range Slider Parameters

Now let’s look at list of available parameters we need to create Range Slider:

ParameterTypeDefaultDescription
elHTMLElement
string
Range Slider element. HTMLElement or string with CSS selector of range slider element
inputElHTMLElement
string
Range Slider input element or CSS selector of input element. If not specified, will try to look for input type=”range” inside of range slider element
dualbooleanfalseEnable dual range slider
stepnumber1Minimal step between values
labelbooleanfalseEnables additional label around range slider knob
minnumberMinimum value
maxnumberMaximum value
valuenumber
array
Initial value. Number in case of single range, and array of values in case of dual range
draggableBarbooleantrueWhen enabled it is also possible to interact with range slider (change value) on range bar click and swipe.
formatLabelfunction(value)Method must return formatted range knob label text. As an argument it receives label value
onobjectObject with events handlers. For example:
  1. var range = app.range.create({
  2. el: ‘.range-slider’,
  3. on: {
  4. change: function () {
  5. console.log(‘Range Slider value changed’)
  6. }
  7. }
  8. })

Range Slider Methods & Properties

So to create Range Slider we have to call:

  1. var range = app.range.create({ /* parameters */ })

After that we have its initialized instance (like range variable in example above) with useful methods and properties:

Properties
range.appLink to global app instance
range.elRange Slider HTML element
range.$elDom7 instance with range slider HTML element
range.inputElRange Slider input HTML element
range.$inputElDom7 instance with range slider input HTML element
range.rangeWidthRange slider width (in px)
range.dualBoolean property indicating whether it is dual or not
range.minRange min value
range.maxRange max value
range.valueRange value
range.knobsArray where each element represents HTMLElement of created range knob (2 knobs in case of dual slider)
range.labelsArray where each element represent HTMLElement of created range knob label (2 labels in case of dual slider)
range.paramsRange Slider parameters
Methods
range.getValue()Returns range slider value
range.setValue(value)Set new range slider value
range.destroy()Destroys range slider instance
range.on(event, handler)Add event handler
range.once(event, handler)Add event handler that will be removed after it was fired
range.off(event, handler)Remove event handler
range.off(event)Remove all handlers for specified event
range.emit(event, …args)Fire event on instance

Range Slider Events

Range Slider will fire the following DOM events on range element and events on app and range instance:

DOM Events

EventTargetDescription
range:changeRange Slider Element<div class=”range-slider”>Event will be triggered when Range Slider value has been changed
range:changedRange Slider Element<div class=”range-slider”>Event will be triggered on slider knob release after value change
range:beforedestroyRange Slider Element<div class=”range-slider”>Event will be triggered right before Range Slider instance will be destroyed

App and Range Slider Instance Events

Range Slider instance emits events on both self instance and app instance. App instance events has same names prefixed with range.

EventArgumentsTargetDescription
change(range)rangeEvent will be triggered when range value has been changed. As an argument event handler receives range instance
rangeChange(range)app
changed(range)rangeEvent will be triggered on slider knob release after value change. As an argument event handler receives range instance
rangeChanged(range)app
beforeDestroy(range)rangeEvent will be triggered right before Range Slider instance will be destroyed. As an argument event handler receives range instance
rangeBeforeDestroy(range)app

Range Slider Auto Initialization

If you don’t need to use Range Slider API and your Range Slider is inside of the page and presented in DOM on moment of page initialization then it can be auto initialized with just adding additional range-slider-init class:

  1. <!-- Add range-slider-init class -->
  2. <div class="range-slider range-slider-init">
  3. <input type="range" min="0" max="100" step="1" value="10">
  4. </div>

In this case if you need to access created Range Slider instance you can use the app.range.get app method:

  1. var range = app.range.get('.range-slider');
  2. if (range.value > 50) {
  3. // do something
  4. }

When using auto init you may need to pass additional parameters. It can be done with two ways:

  • In you case you use single-range slider and you have input:range inside, then step, min, max, value parameters can be set from same input attributes:

    1. <!-- min, max, step, value parameters will be set for same input attributes -->
    2. <div class="range-slider range-slider-init">
    3. <input type="range" min="0" max="100" step="1" value="10">
    4. </div>
  • Otherwise, if you don’t have input:range inside or you use dual range input you can set all available parameters via data- attributes on range slider element.

    1. <!-- parameters set via data- attributes -->
    2. <div
    3. class="range-slider range-slider-init"
    4. data-min="0"
    5. data-max="100"
    6. data-step="10"
    7. data-label="true"
    8. data-value="50"
    9. ></div>

    In case you have dual range slider, then you need to pass to values using data-value-left and data-value-right attributes:

    1. <!-- parameters set via data- attributes -->
    2. <div
    3. class="range-slider range-slider-init"
    4. data-dual="true"
    5. data-min="0"
    6. data-max="100"
    7. data-step="10"
    8. data-label="true"
    9. data-value-left="30"
    10. data-value-right="60"
    11. ></div>

Examples

  1. <div class="block-title">Volume</div>
  2. <div class="list simple-list">
  3. <ul>
  4. <li>
  5. <div class="item-cell width-auto flex-shrink-0">
  6. <i class="icon f7-icons ios-only">volume_mute_fill</i>
  7. <i class="icon material-icons md-only">volume_mute</i>
  8. </div>
  9. <div class="item-cell flex-shrink-3">
  10. <!-- range slider -->
  11. <div class="range-slider range-slider-init">
  12. <input type="range" min="0" max="100" step="1" value="10">
  13. </div>
  14. </div>
  15. <div class="item-cell width-auto flex-shrink-0">
  16. <i class="icon f7-icons ios-only">volume_fill</i>
  17. <i class="icon material-icons md-only">volume_up</i>
  18. </div>
  19. </li>
  20. </ul>
  21. </div>
  1. <div class="block-title">Brightness</div>
  2. <div class="list simple-list">
  3. <ul>
  4. <li>
  5. <div class="item-cell width-auto flex-shrink-0">
  6. <i class="icon f7-icons ios-only">circle</i>
  7. <i class="icon material-icons md-only">brightness_low</i>
  8. </div>
  9. <div class="item-cell flex-shrink-3">
  10. <!-- range slider, enable labels -->
  11. <div class="range-slider range-slider-init color-orange" data-label="true">
  12. <input type="range" min="0" max="100" step="1" value="50">
  13. </div>
  14. </div>
  15. <div class="item-cell width-auto flex-shrink-0">
  16. <i class="icon f7-icons ios-only">circle_half</i>
  17. <i class="icon material-icons md-only">brightness_high</i>
  18. </div>
  19. </li>
  20. </ul>
  21. </div>
  1. <div class="block-title display-flex justify-content-space-between">Price Filter <span class="price-value">$200 - $400</span></div>
  2. <div class="list simple-list">
  3. <ul>
  4. <li class="item-row">
  5. <div class="item-cell width-auto flex-shrink-0">
  6. <i class="icon f7-icons ios-only">money_dollar</i>
  7. <i class="icon material-icons md-only">attach_money</i>
  8. </div>
  9. <div class="item-cell item-cell-shrink-3">
  10. <!-- Dual range slider with all the parameters passed via data- attributes -->
  11. <div
  12. id="price-filter"
  13. class="range-slider range-slider-init color-green"
  14. data-label="true"
  15. data-dual="true"
  16. data-min="0"
  17. data-max="500"
  18. data-step="1"
  19. data-value-left="200"
  20. data-value-right="400"
  21. ></div>
  22. </div>
  23. <div class="item-cell width-auto flex-shrink-0">
  24. <i class="icon f7-icons ios-only">money_dollar_fill</i>
  25. <i class="icon material-icons md-only">monetization_on</i>
  26. </div>
  27. </li>
  28. </ul>
  29. </div>

Handle price change:

  1. $$('#price-filter').on('range:change', function (e, range) {
  2. $$('.price-value').text('$'+(range.value[0])+' - $'+(range.value[1]));
  3. });