如何在SVG文件中构建滑块功能?
我有一个带有彩色条和手柄的滑块的SVG图像(请参见下面的代码段)。我想使此SVG图像交互式,以便查看者可以将手柄移至沿滑块的11个不同位置。一切都需要在< svg>/svg>
标签中;我不能使用外部HTML或脚本。 SVG文件将被添加到< object>
标签中的HTML网页中,以保留交互性。
SVG文件中包含在11个Invisible < rect>
元素,以充当每个11个滑块位置中的hitboxes,以及transform> transform =“ translate(0 0)”
在手柄组上标记。单击其中一个Hitbox,应通过更新transform
参数的X-Value将其移动到相应位置;将鼠标按钮固定下来,即使鼠标垂直移出hitboxes的范围,鼠标也应将手柄移动到对应于鼠标X坐标的位置。
我知道您可以将JavaScript放入SVG文件中,但是我对JS并不熟悉,所以我不知道该如何处理。 JS是最好的方法吗?如果是这样,我该如何使用它来获得所需的功能?
<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>
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>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这是这样的。当代码是独立文档时,该代码看起来应该有所不同,但是您明白了。
这是整个SVG,在对象元素中运行JavaScript:
It is something like this. The code should look a bit different when it is an independent document, but you get the idea.
This is the entire SVG with JavaScript running in an object element: