我想知道是否有可能,如果有的话,是否存在可以与一堆元素一起使用的东西,将相对元素放在所有东西之上? (Z 索引)
大致如下:
$('myElement').moveToFront();
如果我的知识是准确的,这将意味着遍历元素树并更改 Z 索引,以便该元素及其父元素位于其他元素之上。我宁愿不重新发明轮子,也不愿意发现这是一个不可能发明的轮子。是否存在这样做的东西?
最佳答案
那怎么样。
演示
http://jsfiddle.net/insertusernamehere/KNB2f/
注意:该演示仅以一种方式工作(未编写“恢复”功能)。如果您尝试过链接,请重新加载页面。
基本思想
// clone the element and get its current position
var copy = e.clone();
var position = e.offset();
// set the position and a high z-index on the copy
copy .css('position', 'absolute')
.css('z-index', '1000')
.css('top', position.top)
.css('left', position.left);
// append the copy directly to the body
body.append( copy );
// hide the original element, but leave the document flow intact
e.css('visibility', 'hidden');
恢复/撤消此操作:
- 删除副本
- 再次显示原始元素
关于Javascript/小型 Jquery 扩展方法可以自动更改 Z 索引以将元素放在所有内容之上?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11037461/