javascript - SVG 重新排序 z-index(Raphael 可选)

标签 javascript svg raphael

如何在创建后重新排列 Raphael 或其底层 SVG 元素。 更好的是,SVG 中是否存在类似层的东西?

理想情况下,我希望有两层或更多层可以随时放置元素;一个背景层和一个前景层。如果那不是一个选项,将元素弹出到前面就可以了,在这种特殊情况下将它推到后面会更好。

谢谢,

最佳答案

给我密码!

// move element "on top of" all others within the same grouping
el.parentNode.appendChild(el); 

// move element "underneath" all others within the same grouping
el.parentNode.insertBefore(el,el.parentNode.firstChild);

// move element "on top of" all others in the entire document
el.ownerSVGElement.appendChild(el); 

// move element "underneath" all others in the entire document
el.ownerSVGElement.appendChild(el,el.ownerSVGElement.firstChild); 

特别是在 Raphael 中,使用 toBack() 会更容易和 toFront() :

raphElement.toBack()  // Move this element below/behind all others
raphElement.toFront() // Move this element above/in front of all others

详情

SVG 使用 "painters model"绘制对象时:在文档中较晚出现的项目绘制在文档中较早出现的元素之后(在其之上)。要更改项目的分层,您必须使用 appendChild 重新排列 DOM 中的元素。或 insertBefore等等。

您可以在此处查看示例:http://phrogz.net/SVG/drag_under_transformation.xhtml

  1. 拖动红色和蓝色对象,使它们重叠。
  2. 点击每个对象并观察它弹出到顶部。 (但是,黄色圆圈旨在始终可见。)

此示例中元素的重新排序由源代码的第 93/94 行完成:

el.addEventListener('mousedown',function(e){
  el.parentNode.appendChild(el); // move to top
  ...
},false);

当鼠标在一个元素上按下时,它会移动到其所有兄弟元素的最后一个元素,导致它最后绘制,“在所有其他元素之上”。

关于javascript - SVG 重新排序 z-index(Raphael 可选),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6566406/

相关文章:

javascript - 如何从节点中现有的源映射生成新的源映射?

javascript - 无法从 View 访问 Controller 内部的功能

html - 如何将样式应用于嵌入式 SVG?

javascript - 在 raphael.js 中为逆时针圆弧设置动画

javascript - 游戏循环内存泄漏

javascript - 函数 displayOpenXAds() 未定义

javascript - 管道操作符 RXJS Angular 5 的顺序

map - 悬停在特定国家/地区时,在空白世界地图上突出显示国旗

javascript - 在 Canvas 上绘制旋转的 Path2D 对象

java - GWT 可以通过 JSNI 使用 Raphael 吗?