I have a solved question Animating range ticks when moving range thumb. From that question, I have a input type="range"
with custom appearance - thumb is made like an arc (half circle), span
that takes range's value and is designed as a circled thumb and div
with mask
that act like ticks - steps.
From this expected result
I was trying to connect that arc to the slider with a curve. I tried to use pseudo elements but gradient doesn't sync with gradient on the slider and I cannot make a curve like shown in the picture. I also tried using JS canvas to draw that curve and place it on desired place but gradient again doesn't sync - becomes a stationary color.
I thought using a CSS mask but I'm not sure if it's possible to make wanted curve with that.
These are my main research points:
- CSS Tricks - Styling Cross-Browser Compatible Range Inputs
- Gradient Stroke Along Curve in Canvas - Tried to make similar curve (connecting point) with canvas
This is my CodePen and the code
// Position of span that shows range value and tick curve position
const tickContainer = document.getElementById('tickContainer');
const range = document.getElementById('range');
const rangeV = document.getElementById('rangeValue');
const setValue = () => {
// Span position and inner value
const newValue = Number((range.value - range.min) * 100 / (range.max - range.min));
const newPosition = 35 - (newValue * 0.7);
rangeV.style.left = `calc(${newValue}% + (${newPosition}px))`;
rangeV.innerHTML = `<span>${range.value}%</span>`;
// Tick curve position
tickContainer.style.setProperty('--p', `calc(${newValue}%)`);
};
// Initialize setValue onload and oninput
document.addEventListener("DOMContentLoaded", setValue);
range.addEventListener('input', setValue);
body {
font-family: Arial;
margin: 50px;
}
.range-wrap {
position: relative;
}
/* Styling of ticks (lines) over the range */
.ticks {
position: absolute;
left: -15px;
right: -15px;
padding:0 15px;
top: -30px;
height: 45px;
background: repeating-linear-gradient(to right, #D3D3D3 0 1px, transparent 1px 9px);
background-clip:content-box;
-webkit-mask:
radial-gradient(farthest-side at bottom,transparent 75%, #fff 76% 98%, transparent)
var(--p) 0px/100px 50px,
linear-gradient(#fff, #fff) var(--p) 100%/95px 10px,
linear-gradient(#fff, #fff) bottom /100% 10px;
-webkit-mask-repeat: no-repeat;
-webkit-mask-composite: source-over,destination-out;
mask:
radial-gradient(farthest-side at bottom,transparent 75%, #fff 76% 98%, transparent)
var(--p) 0px/100px 50px,
linear-gradient(#fff, #fff) var(--p) 100%/95px 10px,
linear-gradient(#fff, #fff) bottom /100% 10px;
mask-repeat: no-repeat;
mask-composite: exclude;
}
/* Styling the range */
input[type=range] {
-webkit-appearance: none;
appearance: none;
margin: 20px 0;
width: 100%;
height: 4px;
background-image: linear-gradient(125deg, #e0e0e0 34%, rgb(0,12,110) 100%);
outline: none;
transition: all 100ms ease;
}
/* Range track */
input[type=range]::-webkit-slider-runnable-track {
width: 100%;
height: 4px;
cursor: pointer;
border-radius: 25px;
}
input[type=range]::-moz-range-track {
width: 100%;
height: 4px;
cursor: pointer;
border-radius: 25px;
}
/* Range thumb */
input[type=range]::-webkit-slider-thumb {
height: 70px;
width: 70px;
-webkit-transform: translateY(-44.3%) rotate(-45deg);
transform: translateY(-44.3%) rotate(-45deg);
-webkit-appearance: none;
appearance: none;
background: #ddd;
border: 3px solid transparent;
border-color: transparent transparent #fff #fff;
border-radius: 50%;
cursor: pointer;
background-image: linear-gradient(white, white), linear-gradient(to right, #e0e0e0 34%, rgb(0,12,110) 100%);
background-attachment: fixed, fixed;
background-clip: padding-box, border-box;
transition: all 200ms ease;
}
input[type=range]::-moz-range-thumb {
height: 63px;
width: 63px;
appearance: none;
background: #ddd;
border: 3px solid transparent;
transition: all 200ms ease;
border-color: transparent transparent #fff #fff;
border-radius: 50%;
cursor: pointer;
background-image: linear-gradient(white, white), linear-gradient(to right, #e0e0e0 34%, rgb(0,12,110) 100%);
background-attachment: fixed, fixed;
background-clip: padding-box, border-box;
}
/* Range value (label) inside of range thumb */
.range-value {
position: absolute;
top: -50%;
display: -webkit-box;
display: -ms-flexbox;
display: flex;
-webkit-box-pack: center;
-ms-flex-pack: center;
justify-content: center;
-webkit-box-align: center;
-ms-flex-align: center;
align-items: center;
z-index: 99;
user-select: none;
select: none;
pointer-events: none;
}
.range-value span {
width: 50px;
height: 50px;
line-height: 50px;
text-align: center;
color: #fff;
background: rgb(0,12,110);
font-size: 18px;
display: block;
position: absolute;
top: 20px;
border-radius: 50%;
user-select: none;
select: none;
pointer-events: none;
z-index: 100;
}
<div class="range-wrap">
<!-- Ticks (lines) over slider -->
<div class="ticks" id="tickContainer">
</div>
<!-- Range value inside of range thumb -->
<div class="range-value" id="rangeValue"></div>
<!-- Range itself -->
<input id="range" type="range" min="1" max="100" value="5" step="1">
</div>