Creating interactive and visually appealing user interfaces is essential in modern web development. Range sliders are a popular component, often used to let users adjust values dynamically within a range. This article delves into the creation of an Animated Floating Range Slider with a modern design, smooth animations, and a real-time output display. The slider is implemented using HTML, CSS, and JavaScript, ensuring compatibility and responsiveness across devices.
Features of the Animated Floating Range Slider
- Dynamic Value Display: Displays the current value dynamically as the slider moves.
- Smooth Animations: Includes rotation and movement animations for enhanced interactivity.
- Custom Styling: Tailored design for the slider track, thumb, and output display.
- Browser Compatibility: Ensures consistent behavior across major browsers.
- Interactive Feedback: Real-time feedback as users interact with the slider.
Code Implementation
HTML Structure
The core structure of the slider includes an input element of type range and an output element for value display:
<main>
<form oninput="output.value = Math.round(range.valueAsNumber / 1000)">
<h2>Range Slider</h2>
<div class="range">
<input name="range" type="range" min="0" max="100000">
<div class="range-output">
<output class="output" name="output" for="range">50</output>
</div>
</div>
</form>
</main>
CSS Styling
The CSS ensures the slider is visually appealing and interactive. Highlights include:
- Custom styles for the slider track and thumb.
- A floating output display positioned above the thumb.
- Smooth animations for transitions and rotations.
.range {
position: relative;
}
.range input[type=range] {
appearance: none;
width: 100%;
height: 22px;
cursor: pointer;
}
.range input[type=range]:focus {
outline: none;
}
.range input::-webkit-slider-thumb,
.range input::-moz-range-thumb {
width: 24px;
height: 24px;
background: #06486f;
border: 6px solid #fff;
border-radius: 50%;
box-shadow: 0px 4px 25px rgba(0, 0, 0, 0.5);
cursor: pointer;
}
.range .range-output {
position: absolute;
top: -26px;
transform: translate(-50%, -100%);
background: #06486f;
color: #fff;
border-radius: 50%;
padding: 10px;
font-weight: bold;
text-align: center;
}
JavaScript Logic
The JavaScript enhances interactivity by animating the output display and managing value updates.
class RangeInput {
constructor(el) {
this.el = el;
this.sliderThumbEl = el.querySelector('.rangeslider-thumb');
this.outputEl = el.querySelector('.range-output');
this.inputEl = el.querySelector('input[type=range]');
// Initialize slider animation and events
$(this.inputEl).rangeslider({
polyfill: false,
onInit: function() {
$(this.$element)
.parents('.range')
.find('.range-output')
.appendTo(this.$handle);
},
});
}
}
// Initialize the slider on DOM load
new RangeInput(document.querySelector('.range'));
How It Works?
- Dynamic Updates: The slider value updates dynamically via the
oninput
event and is displayed in theoutput
element. - Animation: Rotation effects for the output display add an engaging user experience.
- Customization: The implementation supports various range configurations and styles through CSS and JavaScript.
Benefits
- User-Friendly: Enhances user experience with smooth interactivity and real-time feedback.
- Modern Design: Adds a professional touch to your UI with a polished slider.
- Flexibility: Easily adaptable for different use cases, from e-commerce price filters to volume controls.