我对SVG很陌生,所以我很抱歉,如果这是一个明显的问题。我修了几个小时,好像撞上了一堵墙。如何保持SVG内部元素的纵横比,同时允许SVG本身进行缩放?
我试图创建一个HTML文件,该文件中的SVG元素:
- 在其外视口
- 牢固边界保持内部元件的纵横比
- 不保留方面外SVG元素
- 的比率可以任意地被调整大小,同时保持这些约束上
这说明的jsfiddle我的意思,什么我已经尝试:
下面是相同的代码,因为我不能以其他方式张贴此:
<div>
<h2>correct placement and aspect ratio, but cannot be resized without losing relative placement</h2>
<svg viewBox="0 0 100 100" style="width: 100px; height: 100px; border: 1px solid red;">
<circle cx="0" cy="0" r="10"></circle>
<circle cx="50" cy="50" r="10"></circle>
<circle cx="100" cy="100" r="10"></circle>
</svg>
</div>
<div>
<h2>correct aspect ratio of circle, incorrect relative placement</h2>
<svg viewBox="0 0 100 100" preserveAspectRatio="xMinYMin" style="width: 200px; height: 100px; border: 1px solid red;">
<circle cx="0" cy="0" r="10"></circle>
<circle cx="50%" cy="50%" r="10"></circle>
<circle cx="100%" cy="100%" r="10"></circle>
</svg>
</div>
<div>
<h2>correct relative placement, can be resized, but loses internal aspect ratio</h2>
<svg viewBox="0 0 100 100" preserveAspectRatio="none" style="width: 200px; height: 100px; border: 1px solid red;">
<circle cx="0" cy="0" r="10"></circle>
<circle cx="50" cy="50" r="10"></circle>
<circle cx="100" cy="100" r="10"></circle>
</svg>
</div>
这可能吗?我是否以这种错误的方式去做?
你能解释一下吗? –
我不明白这是如何回答OP的问题。此外'viewBox'在这里是无效的。你不能在'viewBox'中使用百分比值。 –
@PaulLeBeau感谢您纠正我。 – user6683209