I have an SVG image of a slider with a colored bar and a handle (see code snippet below). I want to make this SVG image interactive, so the viewer can move the handle to the eleven different positions along the slider. Everything needs to be within the <svg></svg> tags; I can't use external HTML or scripts. The SVG file will be added to an HTML webpage in an <object> tag to preserve interactivity.
Included in the SVG file are eleven invisible <rect> elements to act as hitboxes for each of the eleven slider positions, and a transform="translate(0 0)" tag on the handle group. Clicking in one of the hitboxes should move the handle to the corresponding position by updating the transform parameter's x-value; with the mouse button held down, moving the mouse from side to side should move the handle to the position corresponding to the x-coordinate of the mouse, even if the mouse moves vertically out of range of the hitboxes.
I know you can put JavaScript inside SVG files, but I'm not very familiar with JS so I don't know how to approach this. Is JS the best way? If so, how do I use it to get the desired functionality?
<svg id="Slider-Image" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" width="240" height="84" viewBox="0 0 240 84">
<defs>
<style>
.hitbox {
cursor: pointer;
opacity: 0;
}
</style>
</defs>
<g id="Slider-BG">
<rect id="BG-Fill" width="240" height="84" fill="#999"/>
<polygon id="Bevel-Top" points="9 29 14 35 226 35 231 29 9 29" fill="#887"/>
<polygon id="Bevel-Right" points="226 35 226 63 231 69 231 29 226 35" fill="#aba"/>
<polygon id="Bevel-Left" points="9 29 9 69 14 63 14 35 9 29" fill="#baa"/>
<polygon id="Bevel-Bottom" points="9 69 231 69 226 63 14 63 9 69" fill="#cdd"/>
</g>
<g id="Slider-Bar">
<rect id="Slider-Bar-BG" x="14" y="35" width="212" height="28"/>
<rect id="Slider-Bar-Red" x="15" y="38" width="18" height="19" fill="#f22"/>
<rect id="Slider-Bar-Yellow" x="35" y="38" width="18" height="19" fill="#ec0"/>
<rect id="Slider-Bar-Green" x="53" y="38" width="171" height="19" fill="#9e1"/>
<line id="Slider-Bar-Divider-1" x1="53.5" y1="38" x2="53.5" y2="57" stroke="#000" opacity="0.5"/>
<line id="Slider-Bar-Divider-2" x1="72.5" y1="38" x2="72.5" y2="57" stroke="#000" opacity="0.5"/>
<line id="Slider-Bar-Divider-3" x1="91.5" y1="38" x2="91.5" y2="57" stroke="#000" opacity="0.5"/>
<line id="Slider-Bar-Divider-4" x1="110.5" y1="38" x2="110.5" y2="57" stroke="#000" opacity="0.5"/>
<line id="Slider-Bar-Divider-5" x1="129.5" y1="38" x2="129.5" y2="57" stroke="#000" opacity="0.5"/>
<line id="Slider-Bar-Divider-6" x1="148.5" y1="38" x2="148.5" y2="57" stroke="#000" opacity="0.5"/>
<line id="Slider-Bar-Divider-7" x1="167.5" y1="38" x2="167.5" y2="57" stroke="#000" opacity="0.5"/>
<line id="Slider-Bar-Divider-8" x1="186.5" y1="38" x2="186.5" y2="57" stroke="#000" opacity="0.5"/>
<line id="Slider-Bar-Divider-9" x1="205.5" y1="38" x2="205.5" y2="57" stroke="#000" opacity="0.5"/>
</g>
<g id="Slider-Handle" transform="translate(0 0)">
<polygon id="Handle-Body" points="31 66 57 66 57 46 46 35 42 35 31 46 31 66" fill="#aaa"/>
<rect id="Handle-Center" x="42" y="35" width="4" height="31" fill="#ddd"/>
<rect id="Handle-Pointer" x="43" y="34" width="2" height="16" fill="#111"/>
</g>
<g id="Slider-Positions">
<rect id="Slider-Position-Minus-20" class="hitbox" x="15" y="29" width="19" height="40"/>
<rect id="Slider-Position-Plus-0" class="hitbox" x="34" y="29" width="19" height="40"/>
<rect id="Slider-Position-Plus-19" class="hitbox" x="53" y="29" width="19" height="40"/>
<rect id="Slider-Position-Plus-38" class="hitbox" x="72" y="29" width="19" height="40"/>
<rect id="Slider-Position-Plus-57" class="hitbox" x="91" y="29" width="19" height="40"/>
<rect id="Slider-Position-Plus-76" class="hitbox" x="110" y="29" width="19" height="40"/>
<rect id="Slider-Position-Plus-95" class="hitbox" x="129" y="29" width="19" height="40"/>
<rect id="Slider-Position-Plus-114" class="hitbox" x="148" y="29" width="19" height="40"/>
<rect id="Slider-Position-Plus-133" class="hitbox" x="167" y="29" width="19" height="40"/>
<rect id="Slider-Position-Plus-152" class="hitbox" x="186" y="29" width="19" height="40"/>
<rect id="Slider-Position-Plus-171" class="hitbox" x="205" y="29" width="19" height="40"/>
</g>
</svg>