SVG 重新排序 z 索引(Raphael 可选)
创建后如何重新排序 Raphael 或其底层 SVG 元素。 更好的是,SVG 中是否存在像图层这样的东西?
理想情况下,我希望有两个或多个层可以随时放置元素;背景层和前景层。如果这不是一个选择,那么将元素弹出到前面就可以了,在这种特殊情况下将其推到后面会更好。
谢谢,
How can I reorder Raphael or their underlying SVG elements after creation.
Better yet, do something like layers exist in SVG?
Ideally I would like two or more layers in which to place elements at any time; A background and a foreground layer. If that is not an option, popping elements to the front would be ok, and pushing it to the back would be better in this particular case.
Thanks,
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
给我代码!
特别是在 Raphael 中,使用
toBack()
和toFront()
:详细信息
SVG 使用 "画家模型" 绘制对象时:文档中稍后出现的项目将在之后绘制(在) 文档中较早出现的元素。要更改项目的分层,您必须使用
appendChild
或insertBefore
等。您可以在此处查看示例: http://phrogz.net/SVG/drag_under_transformation.xhtml
此示例中的元素重新排序是由源代码的第 93/94 行完成的:
当鼠标在元素上按下时,它会移动到它所有兄弟元素的最后一个元素,导致它最后绘制,“在所有其他元素之上”。
Gimme the Code!
Within Raphael specifically, it's even easier by using
toBack()
andtoFront()
:Details
SVG uses a "painters model" when drawing objects: items that appear later in the document are drawn after (on top of) elements that appear earlier in the document. To change the layering of items, you must re-order the elements in the DOM, using
appendChild
orinsertBefore
or the like.You can see an example of this here: http://phrogz.net/SVG/drag_under_transformation.xhtml
The re-ordering of elements on this example is done by lines 93/94 of the source code:
When the mouse is pushed down on an element, it is moved to be the last element of all its siblings, causing it to draw last, "on top" of all others.
如果您使用 Raphael,向后和向前弹出元素非常简单:
这是相关文档。
If you're using Raphael, popping elements backwards and forwards is very straightforward:
Here's the relevant documentation.
SVG 中没有 z 索引,代码中后面的对象出现在第一个对象的上方。因此,根据您的需要,您可以将节点移动到树的开头或结尾。
(group) 元素是 svg 中的通用容器,因此它们可以是您的图层。只需在组之间移动节点即可实现您的需要。There's no z-index in SVG, objects that are later in the code appear above the first ones. So for your needs, you can move the node to the start of the tree or the end of it.
<g>
(group) element is a generic container in svg, so they can be layers for you. Just move nodes between the groups to achieve what you need.如果您预定义图形对象,则可以随着时间的推移以不同的顺序对它们进行分层:
If you predefine your graphic objects, you can then layer them in different orders as time evolves:
我还没有尝试过,但是 SVG 渲染顺序 看起来可以成为一个解决方案。而且“z-index”即将到来,它已经在 提案中
I haven't tried it yet but SVG render order looks like it could be a solution. And also 'z-index' is coming, it is already in proposal
实际上,我认为单独的appendChild()会复制元素,而不仅仅是移动它。这可能不是问题,但如果您想避免重复,我建议如下:
Actually, I think appendChild() alone will copy the element and not just move it. This may not be a problem but if you want to avoid duplicates I suggest something like this: