Skip to content

Slider

A customizable slider component that allows users to select a value from a range by dragging a handle along a track. The slider can be configured in various ways, including horizontal or vertical orientation, with or without hints, and with different visual styles.

This component has no external dependencies beyond the Metro UI core.

<input data-role="slider">
<input data-role="slider" data-vertical="true">
<input data-role="slider" data-show-min-max="true" data-min="0" data-max="100">
<input data-role="slider" data-hint="true" data-hint-always="true" data-hint-position="top">
<input data-role="slider" data-min="-100" data-max="100" data-value="0">
<input data-role="slider" data-accuracy="5" data-min="0" data-max="100">
<input data-role="slider" data-buffer="25">
<!-- Thin slider -->
<input data-role="slider" class="thin">
<!-- Ultra-thin slider -->
<input data-role="slider" class="ultra-thin">
<!-- Rounded slider -->
<input data-role="slider" class="rounded">
<!-- Hidden button (shows on hover) -->
<input data-role="slider" data-hide-button="true">
<!-- Circular marker -->
<input data-role="slider" class="cycle-marker">
ParameterTypeDefaultDescription
minnumber0Minimum value of the slider
maxnumber100Maximum value of the slider
accuracynumber0Step size for the slider value (0 means no stepping)
valuenumber0Initial value of the slider
buffernumber0Initial buffer value (0-100)
roundValuebooleantrueWhether to round the value according to accuracy
showMinMaxbooleanfalseWhether to show min and max labels
minMaxPositionstring”top”Position of min/max labels (“top” or “bottom”)
hintbooleanfalseWhether to show a hint with the current value
hintAlwaysbooleanfalseWhether to always show the hint (not just during interaction)
hintPositionstring”top”Position of the hint (“top”, “bottom”, “left”, or “right”)
hintMaskstring”$1”Mask for the hint text ($1 = value, $2 = percent)
verticalbooleanfalseWhether the slider is vertical
targetstringnullSelector for target element(s) to update with the slider value
returnTypestring”value”Type of value to return (“value” or “percent”)
sizenumber0Size of the slider (width for horizontal, height for vertical)
labelstringnullLabel text for the slider
hideButtonbooleanfalseWhether to hide the slider button (shows on hover)
clsSliderstring""Additional CSS class for the slider
clsBacksidestring""Additional CSS class for the backside element
clsCompletestring""Additional CSS class for the complete element
clsBufferstring""Additional CSS class for the buffer element
clsMarkerstring""Additional CSS class for the marker element
clsHintstring""Additional CSS class for the hint element
clsMinMaxstring""Additional CSS class for the min/max wrapper
clsMinstring""Additional CSS class for the min label
clsMaxstring""Additional CSS class for the max label
  • val(v) - Get or set the slider value. If v is undefined, returns the current value.
  • buff(v) - Get or set the buffer value (0-100). If v is undefined, returns the current buffer value.
  • changeValue() - Change the slider value based on the data-value attribute.
  • changeBuffer() - Change the buffer value based on the data-buffer attribute.
  • disable() - Disable the slider.
  • enable() - Enable the slider.
  • toggleState() - Toggle between enabled and disabled states.
  • destroy() - Remove the slider and clean up events.
const slider = Metro.getPlugin('#mySlider', 'slider');
slider.val(50); // Set value to 50
slider.buff(25); // Set buffer to 25%
slider.disable(); // Disable the slider
EventDescription
onStartTriggered when the user starts dragging the slider
onStopTriggered when the user stops dragging the slider
onMoveTriggered when the slider is being moved
onSliderClickTriggered when the slider track is clicked
onChangeTriggered when the slider value or buffer changes
onChangeValueTriggered when the slider value changes
onChangeBufferTriggered when the buffer value changes
onFocusTriggered when the slider receives focus
onBlurTriggered when the slider loses focus
onSliderCreateTriggered when the slider is created
<input data-role="slider"
data-on-change="console.log('Value changed to: ' + arguments[0].val)"
data-on-start="console.log('Started dragging')">
VariableDefault (Light)Dark ModeDescription
--slider-thumb-size18px18pxSize of the slider thumb
--slider-thumb-colorvar(—color-blue)var(—color-crimson)Color of the slider thumb
--slider-bar-color#191919#191919Color of the slider bar
--slider-buffer-color#fefefe#fefefeColor of the buffer
--slider-back-color#d5d5d5#4e5055Background color of the slider
--slider-fill-colorvar(—color-cyan)#ff145cColor of the filled part
--slider-thumb-border-colorvar(—color-light-cyan)#ffffffBorder color of the thumb
#my-slider {
--slider-thumb-size: 24px;
--slider-thumb-color: #ff5722;
--slider-fill-color: #ff9800;
--slider-back-color: #f5f5f5;
}
  • .slider - The main container class (automatically added)
  • .thin - Creates a thinner slider (6px height)
  • .ultra-thin - Creates an ultra-thin slider (4px height)
  • .cycle-marker - Makes the marker circular
  • .vertical-slider - Vertical orientation (automatically added when vertical=true)
  • .rounded - Adds rounded corners to the slider elements
  • .hidden-button - Hides the marker button until hover (automatically added when hideButton=true)
  • .disabled - Disabled state (automatically added when the slider is disabled)
  • .backside - The background track of the slider
  • .complete - The filled part of the slider
  • .buffer - The buffer indicator
  • .marker - The draggable handle
  • .hint - The tooltip showing the current value
  • .slider-min-max - Container for min/max labels
  • .slider-text-min - The minimum value label
  • .slider-text-max - The maximum value label

The slider has the following structure:

div.slider
├── div.backside
├── div.complete
├── div.buffer
└── button.marker
└── div.hint

When min/max labels are enabled:

div.slider-min-max
├── span.slider-text-min
└── span.slider-text-max
div.slider
...
<div class="example">
<input id="mySlider" data-role="slider"
data-min="0"
data-max="100"
data-value="50"
data-buffer="75"
data-hint="true"
data-hint-always="true"
data-hint-mask="Value: $1 ($2%)"
data-show-min-max="true"
data-accuracy="5"
class="thin rounded"
data-on-change="console.log('Value: ' + arguments[0].val + ', Buffer: ' + arguments[0].buffer)">
</div>
<script>
// Access the slider via JavaScript
const slider = Metro.getPlugin('#mySlider', 'slider');
// Change the value programmatically
slider.val(25);
// Change the buffer programmatically
slider.buff(50);
</script>

This example creates a slider with a range of 0-100, an initial value of 50, a buffer of 75%, a permanent hint showing the current value, min/max labels, and a step size of 5. The slider has a thin, rounded style and logs changes to the console.