Below is a complete example of a circular range slider implemented using HTML, CSS, and JavaScript, all bundled into a single HTML file. This slider features a circular path for the thumb to move along and displays the selected value (ranging from 0 to 100) at the center of the circle.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Circular Range Slider</title>
<style>
/* Styles for the circular slider */
.circular-slider {
position: relative;
width: 300px;
height: 300px;
margin: 50px auto;
}
.circular-slider svg {
transform: rotate(-90deg); /* Start from top */
}
.circular-slider .circle-bg {
fill: none;
stroke: #eee;
stroke-width: 3.8;
}
.circular-slider .circle-progress {
fill: none;
stroke: #03a9f4;
stroke-width: 2.8;
stroke-linecap: round;
transition: stroke-dashoffset 0.3s ease;
}
.circular-slider .thumb {
position: absolute;
width: 20px;
height: 20px;
border-radius: 50%;
background: #03a9f4;
cursor: grab;
transition: transform 0.1s ease;
}
.circular-slider .value-text {
position: absolute;
top: 50%;
left: 50%;
transform: translate(-50%, -50%);
font-size: 36px;
font-weight: bold;
color: #333;
}
</style>
</head>
<body>
<div class="circular-slider">
<svg width="100%" height="100%" viewBox="0 0 100 100">
<circle class="circle-bg" cx="50" cy="50" r="45"></circle>
<circle class="circle-progress" cx="50" cy="50" r="45" stroke-dasharray="283" stroke-dashoffset="283"></circle>
</svg>
<div class="thumb"></div>
<div class="value-text">0</div>
</div>
<script>
const circularSlider = document.querySelector('.circular-slider');
const circleProgress = circularSlider.querySelector('.circle-progress');
const thumb = circularSlider.querySelector('.thumb');
const valueText = circularSlider.querySelector('.value-text');
let isDragging = false;
let centerX = 150; // Since width is 300px, center is at 150px
let centerY = 150; // Since height is 300px, center is at 150px
let radius = 135; // Radius of the circle path (slightly less than 150 for padding)
// Function to update the slider value
function updateSlider(value) {
const circumference = 2 * Math.PI * 45; // 45 is the radius defined in SVG circle
const offset = circumference - (value / 100) * circumference;
circleProgress.style.strokeDashoffset = offset;
valueText.innerText = Math.round(value);
// Calculate the angle in radians then convert to degrees
const angleDeg = (value / 100) * 360;
const angleRad = angleDeg * Math.PI / 180;
// Position the thumb
const thumbX = centerX + radius * Math.cos(angleRad) - 10; // -10 for thumb width adjustment
const thumbY = centerY + radius * Math.sin(angleRad) - 10; // -10 for thumb height adjustment
thumb.style.transform = `translate(${thumbX}px, ${thumbY}px)`;
}
// Initial setup
updateSlider(0);
// Mouse events for dragging
thumb.addEventListener('mousedown', (e) => {
isDragging = true;
e.preventDefault();
});
document.addEventListener('mouseup', () => isDragging = false);
document.addEventListener('mousemove', (e) => {
if (!isDragging) return;
const rect = circularSlider.getBoundingClientRect();
const x = e.clientX - rect.left;
const y = e.clientY - rect.top;
// Calculate the angle from center
let dx = x - centerX;
let dy = y - centerY;
let angleRad = Math.atan2(dy, dx);
let angleDeg = (angleRad * 180 / Math.PI) + 90; // Normalize the angle
if (angleDeg < 0) angleDeg += 360;
// Map angle to value (0-360 degrees to 0-100 value)
let value = Math.min(Math.max((angleDeg / 360) * 100, 0), 100);
updateSlider(value);
});
// Touch events for dragging on mobile devices
thumb.addEventListener('touchstart', (e) => {
isDragging = true;
e.preventDefault();
});
document.addEventListener('touchend', () => isDragging = false);
document.addEventListener('touchmove', (e) => {
if (!isDragging) return;
const touch = e.touches[0];
const rect = circularSlider.getBoundingClientRect();
const x = touch.clientX - rect.left;
const y = touch.clientY - rect.top;
let dx = x - centerX;
let dy = y - centerY;
let angleRad = Math.atan2(dy, dx);
let angleDeg = (angleRad * 180 / Math.PI) + 90;
if (angleDeg < 0) angleDeg += 360;
let value = Math.min(Math.max((angleDeg / 360) * 100, 0), 100);
updateSlider(value);
});
</script>
</body>
</html>
This code does the following:
- Creates a circular slider component with SVG for the circular path.
- Uses two circles in SVG: one for the background (
circle-bg) and one for the progress indication (circle-progress).
- Positions a
div (.thumb) absolutely to act as the draggable handle, and another div (.value-text) to display the current value at the center.
- Implements dragging logic with both mouse and touch events to make the slider work on desktop and mobile devices.
- Updates the progress circle and thumb position based on the calculated angle from the center when the user drags the thumb.
You can save this as an .html file and open it in any modern web browser to see it in action. Enjoy experimenting with the circular range slider!