In version 4.0.0 we added the ability to have a float on the range (area between handles). This is useful for
when you want to show the current value of the range.
Hover/Drag the Slider handles below to see the float
<RangeSliderrangeFloatfloat />
Always show the floats
We can set the floats to always be visible by modifying the css slightly. This can either
be done in an external css file or inline in the Svelte component.
<RangeSliderid="always"floatrangebind:values /><style>/* to style inside of a Svelte component, we need to use :global() */ :global(#always.rangeFloat) {opacity: 1;translate: -50%0%; }</style>
/* styling in an external css file */#always.rangeFloat {opacity: 1;translate: -50%0%;}
Focus on load
It’s possible to focus the slider on load with a little bit of javascript.
<script>let slider;$: if ( slider ) { slider.querySelector('.rangeHandle').focus(); }</script><RangeSliderbind:sliderfloat />
Automatically toggle rangeFloat
Here we demonstrate a simple example of how to toggle the rangeFloat and float properties
based on the distance between the handles.
<script>let values = [25,75];</script><RangeSliderid="toggle-range-float"bind:valuesrangerangeFloat={values[1] - values[0] <22} float={values[1] - values[0] >=22} /><p>Try dragging the handles close to each other</p>
This simple example is using the values property to toggle the rangeFloat and float properties.
But it would be possible to use the on:change event, and calculate the pixel-distance between the handles,
if that was a better approach for your use case.
Keep Float inside container
If your slider has very long float labels—and is positioned close to the edge
of the browser or its parent container—you may find the floats extend past the
boundaries and look terrible.
One way to keep floats inside is by using a simple css calc() to re-position the
floats based on the handle’s position.
<script>let value =80;</script><divclass="container"> <RangeSliderid="auto-position"bind:valuefloathandleFormatter={value=>`Stays Inside Container @ ${(value*-0.6-20).toFixed(1)}%`} /> <p>Move the handle from left-right</p> <RangeSliderbind:valuefloathandleFormatter={value=>`Overflows Container @ ${value}%`} /></div><style>.container {overflow: hidden;padding-inline: 1em; } :global(.container.rangeSlider.rangeFloat) {opacity: 1;translate: -50%-20%0.01px; }</style>
#auto-position.rangeFloat {--float-x: calc((var(--handle-pos) *-0.6%-20%)); /* offset the float horizontally */--float-y: -20%; /* offset the float vertically *//* keep the float inside the container by shifting it based on handle's position */translate: var(--float-x) var(--float-y) 0.01px;}