Created
March 27, 2022 01:42
-
-
Save justinpenner/295efd412d5e266ed58a155785a0e209 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Add units to a number | |
function addUnits(n, unitsFormat) { | |
return unitsFormat.replace('{}',n); | |
} | |
// Slider slide event | |
function slide(event, samp) { | |
// Apply style to samp | |
samp.style[event.srcElement.dataset.prop] = addUnits(event.srcElement.value, event.srcElement.dataset.units); | |
// Update other controls that control the same property (i.e. slider and number input) | |
event.srcElement.parentElement.querySelectorAll( | |
`input[data-prop=${event.srcElement.dataset.prop}` | |
).forEach( (input) => { | |
input.value = event.srcElement.value; | |
}); | |
} | |
// Activate the event listener and dispatch it once to sync up the UI controls | |
sliders.size.addEventListener('input', (event) => slide(event,samp)); | |
sliders.size.dispatchEvent(new Event('input')); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment