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
formatLabelfunction(value)Method must return formatted range knob label text. As an argument it receives label value
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.
verticalbooleanfalseEnables vertical range slider
verticalReversedbooleanfalseMakes vertical range slider reversed (vertical must be also enabled)
scalebooleanfalseEnables range slider scale
scaleStepsnumber5Number of scale steps
scaleSubStepsnumber0Number of scale sub steps (each step will be divided by this value)
formatScaleLabelfunction (value)Method must return formatted scale value. As an argument it receives currect scale step value. This method will be called as for each scale step.
limitKnobPositionbooleanLimits knob position to the size of the range bar. By default enabled from iOS theme
onobject

Object 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.scaleBoolean property indicating whether scale is enabled or not
range.scaleStepsNumber of scale steps
range.scaleSubStepsNumber of scale sub steps
range.$scaleElDom7 instance with generated scale HTML element
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.updateScale()Re-calculate and re-render slider scale
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, value)rangeEvent will be triggered when range value has been changed. As an argument event handler receives range instance
rangeChange(range, value)app
changed(range, value)rangeEvent will be triggered on slider knob release after value change. As an argument event handler receives range instance
rangeChanged(range, value)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>

CSS Variables

Below is the list of related CSS variables (CSS custom properties).

Note that commented variables are not specified by default and their values is what they fallback to in this case.

  1. :root {
  2. /*
  3. --f7-range-bar-active-bg-color: var(--f7-theme-color);
  4. --f7-range-scale-bg-color: var(--f7-range-bar-bg-color);
  5. --f7-range-scale-substep-bg-color: var(--f7-range-bar-bg-color);
  6. */
  7. --f7-range-scale-step-height: 5px;
  8. --f7-range-scale-text-color: #666;
  9. --f7-range-scale-substep-width: 1px;
  10. --f7-range-scale-substep-height: 4px;
  11. }
  12. .ios {
  13. --f7-range-size: 28px;
  14. --f7-range-bar-bg-color: #b7b8b7;
  15. --f7-range-bar-size: 1px;
  16. --f7-range-bar-border-radius: 2px;
  17. --f7-range-knob-size: 28px;
  18. --f7-range-knob-color: #fff;
  19. --f7-range-knob-box-shadow: 0 2px 4px rgba(0, 0, 0, 0.3);
  20. --f7-range-label-size: 20px;
  21. --f7-range-label-text-color: #000;
  22. --f7-range-label-bg-color: #fff;
  23. --f7-range-label-font-size: 12px;
  24. --f7-range-label-font-weight: normal;
  25. --f7-range-label-border-radius: 5px;
  26. --f7-range-label-padding: 0px;
  27. --f7-range-scale-step-width: 1px;
  28. --f7-range-scale-font-size: 12px;
  29. --f7-range-scale-font-weight: 400;
  30. --f7-range-scale-label-offset: 4px;
  31. }
  32. .md {
  33. --f7-range-size: 20px;
  34. --f7-range-bar-bg-color: #b9b9b9;
  35. --f7-range-bar-size: 2px;
  36. --f7-range-bar-border-radius: 0px;
  37. --f7-range-knob-size: 12px;
  38. /*
  39. --f7-range-knob-color: var(--f7-theme-color);
  40. */
  41. --f7-range-knob-box-shadow: none;
  42. --f7-range-label-size: 26px;
  43. --f7-range-label-font-weight: normal;
  44. --f7-range-label-text-color: #fff;
  45. /*
  46. --f7-range-label-bg-color: var(--f7-theme-color);
  47. */
  48. --f7-range-label-font-size: 10px;
  49. --f7-range-label-border-radius: 50%;
  50. --f7-range-label-padding: 0px;
  51. --f7-range-scale-step-width: 2px;
  52. --f7-range-scale-font-size: 12px;
  53. --f7-range-scale-font-weight: 400;
  54. --f7-range-scale-label-offset: 4px;
  55. }
  56. .aurora {
  57. --f7-range-size: 20px;
  58. --f7-range-bar-bg-color: #c7c7c7;
  59. --f7-range-bar-size: 2px;
  60. --f7-range-bar-border-radius: 2px;
  61. --f7-range-knob-size: 16px;
  62. /*
  63. --f7-range-knob-color: var(--f7-theme-color);
  64. */
  65. --f7-range-knob-box-shadow: none;
  66. --f7-range-label-size: 20px;
  67. --f7-range-label-text-color: #fff;
  68. /*
  69. --f7-range-label-bg-color: var(--f7-theme-color);
  70. */
  71. --f7-range-label-font-size: 10px;
  72. --f7-range-label-font-weight: 600;
  73. --f7-range-label-border-radius: 4px;
  74. --f7-range-label-padding: 0px 4px;
  75. --f7-range-scale-step-width: 2px;
  76. --f7-range-scale-font-size: 11px;
  77. --f7-range-scale-font-weight: 500;
  78. --f7-range-scale-label-offset: 2px;
  79. }

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 if-not-md">speaker_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 if-not-md">speaker_3_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 if-not-md">sun_min</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 if-not-md">sun_max</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 if-not-md">money_dollar_round</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 if-not-md">money_dollar_round_fill</i>
  25. <i class="icon material-icons md-only">monetization_on</i>
  26. </div>
  27. </li>
  28. </ul>
  29. </div>
  30. <div class="block-title">With Scale</div>
  31. <div class="block block-strong">
  32. <div class="range-slider range-slider-init"
  33. data-min="0"
  34. data-max="100"
  35. data-label="true"
  36. data-step="5"
  37. data-value="25"
  38. data-scale="true"
  39. data-scale-steps="5"
  40. data-scale-sub-steps="4"
  41. ></div>
  42. </div>
  43. <div class="block-title">Vertical</div>
  44. <div class="block block-strong display-flex justify-content-center">
  45. <div class="range-slider range-slider-init margin-right"
  46. data-vertical="true"
  47. data-min="0"
  48. data-max="100"
  49. data-label="true"
  50. data-step="1"
  51. data-value="25"
  52. style="height: 160px"
  53. ></div>
  54. <div class="range-slider range-slider-init margin-horizontal"
  55. data-vertical="true"
  56. data-min="0"
  57. data-max="100"
  58. data-label="true"
  59. data-step="1"
  60. data-value="50"
  61. style="height: 160px"
  62. ></div>
  63. <div class="range-slider range-slider-init margin-horizontal"
  64. data-vertical="true"
  65. data-min="0"
  66. data-max="100"
  67. data-label="true"
  68. data-step="1"
  69. data-value="75"
  70. style="height: 160px"
  71. ></div>
  72. <div class="range-slider range-slider-init margin-left"
  73. data-dual="true"
  74. data-vertical="true"
  75. data-min="0"
  76. data-max="100"
  77. data-label="true"
  78. data-step="1"
  79. data-value-left="25"
  80. data-value-right="75"
  81. style="height: 160px"
  82. ></div>
  83. </div>
  84. <div class="block-title">Vertical Reversed</div>
  85. <div class="block block-strong display-flex justify-content-center">
  86. <div class="range-slider range-slider-init color-red margin-right"
  87. data-vertical="true"
  88. data-vertical-reversed="true"
  89. data-min="0"
  90. data-max="100"
  91. data-label="true"
  92. data-step="1"
  93. data-value="25"
  94. style="height: 160px"
  95. ></div>
  96. <div class="range-slider range-slider-init color-red margin-horizontal"
  97. data-vertical="true"
  98. data-vertical-reversed="true"
  99. data-min="0"
  100. data-max="100"
  101. data-label="true"
  102. data-step="1"
  103. data-value="50"
  104. style="height: 160px"
  105. ></div>
  106. <div class="range-slider range-slider-init color-red margin-horizontal"
  107. data-vertical="true"
  108. data-vertical-reversed="true"
  109. data-min="0"
  110. data-max="100"
  111. data-label="true"
  112. data-step="1"
  113. data-value="75"
  114. style="height: 160px"
  115. ></div>
  116. <div class="range-slider range-slider-init color-red margin-left"
  117. data-dual="true"
  118. data-vertical="true"
  119. data-vertical-reversed="true"
  120. data-min="0"
  121. data-max="100"
  122. data-label="true"
  123. data-step="1"
  124. data-value-left="25"
  125. data-value-right="75"
  126. style="height: 160px"
  127. ></div>
  128. </div>

Handle price change:

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

← Radio

Searchbar →