Use CSS custom property for webkit range fill

This commit is contained in:
Sam Potts
2018-01-08 20:53:18 +11:00
parent f0322e8d94
commit 8786377a75
8 changed files with 15 additions and 36 deletions

26
src/js/controls.js vendored
View File

@ -26,30 +26,8 @@ const controls = {
return;
}
// Inject the stylesheet if needed
if (!utils.is.element(this.elements.styleSheet)) {
this.elements.styleSheet = utils.createElement('style');
this.elements.container.appendChild(this.elements.styleSheet);
}
const styleSheet = this.elements.styleSheet.sheet;
const percentage = range.value / range.max * 100;
const selector = `#${range.id}::-webkit-slider-runnable-track`;
const styles = `{ background-image: linear-gradient(to right, currentColor ${percentage}%, transparent ${percentage}%) }`;
// Find old rule if it exists
const index = Array.from(styleSheet.rules).findIndex(rule => rule.selectorText === selector);
// Remove old rule
if (index !== -1) {
styleSheet.deleteRule(index);
}
// Insert new one
styleSheet.insertRule([
selector,
styles,
].join(' '));
// Set CSS custom property
range.style.setProperty('--value', `${range.value / range.max * 100}%`);
},
// Get icon URL