P粉3871087722023-09-04 12:20:01
I'm not sure if this helps, but here's the refactoring I did. Maybe you can make some modifications to suit your task.
HTML
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta http-equiv="X-UA-Compatible" content="IE=edge"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <link rel="stylesheet" href="style.css"> <title>Slider</title> </head> <body> <div> <span id="rangeValue">0</span> <Input type="range" name="" id="range" value="0" min="0" max="100"></Input> </div> </body> <script src=""></script> </html>
CSS
body { justify-content: center; align-items: center; min-height: 100vh; background: #151515; } #rangeValue { position: relative; display: block; font-size: 6em; color: #999; font-weight: 400; } /* Input Thumb */ input[type="range"]::-webkit-slider-thumb { -webkit-appearance: none; height: 20px; width: 20px; border-radius: 50%; background: #ff4500; cursor: ew-resize; box-shadow: 0 0 2px 0 #555; transition: background .3s ease-in-out; } input[type="range"]::-moz-range-thumb { -webkit-appearance: none; height: 20px; width: 20px; border-radius: 50%; background: #ff4500; cursor: ew-resize; box-shadow: 0 0 2px 0 #555; transition: background .3s ease-in-out; } input[type="range"]::-ms-thumb { -webkit-appearance: none; height: 20px; width: 20px; border-radius: 50%; background: #ff4500; cursor: ew-resize; box-shadow: 0 0 2px 0 #555; transition: background .3s ease-in-out; } /* Input Track */ input[type=range]::-webkit-slider-runnable-track { -webkit-appearance: none; box-shadow: none; border: none; background: transparent; } input[type=range]::-moz-range-track { -webkit-appearance: none; box-shadow: none; border: none; background: transparent; }
JS
const rangeInputs = document.querySelectorAll('input[type="range"]') function handleInputChange(e) { let target = e.target if (e.target.type !== 'range') { target = document.getElementById('range') } const min = target.min const max = target.max const val = target.value target.style.backgroundSize = (val - min) * 100 / (max - min) + '% 100%' } rangeInputs.forEach(input => { input.addEventListener('input', handleInputChange) })
You can adjust it to your liking. Hope this helps.