使用 CSS/Javascript 创建堆栈视图控件
我想创建一个堆栈类型控件,其中一系列堆叠的子 div 可以在容器 div 内重新排序。
子分区“视图”的大小相同,并且绝对位于彼此之上。
z-index css 属性似乎在文档级别应用 z 顺序,这对我来说毫无意义 - 给定 div 中的元素如何具有比放置在该 div 之后的元素更高的 z 顺序?它只是忽略了嵌套的 div 层次结构?
所以问题是:有没有办法在给定的 div 内操纵相对 z 顺序?
谢谢, 亚林
I Want to create a stack type control where a series of stacked sub-divs can be re-ordered within a container div.
The sub-div 'views' would be the same size and absolutely positioned on top of each other.
The z-index css property seems to apply z-order at the document level, which makes no sense to me- How can an element in a given div have a higher z-order than something that is placed after that div? It just ignores the nested div heirarchy?
So the question is: Is there a way to manipulate relative z-order within a given div?
Thanks,
Yarin
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
如果您只希望一个 div 可见,则将其不透明度在 Firefox 中更改为 1,在 IE 中更改为 100,并将其他设置为 0。这是一个 幻灯片放映示例就是这样做的。我想你的不会基于计时器,所以你需要一些其他方法来切换它们。
If you want only one div to be visible then change its opacity to 1 in Firefox and 100 in IE and set the others to 0. Here's a slide show example that does just that. I would imagine yours wouldn't be based on a timer so you would need some other method to switch them.
Z-index
仅修改显示元素的“图层”(想象一下 Photoshop)。从结构上来说,盒子模型没有改变。从视觉上看,似乎是这样,但前提是元素的位置已被修改(通过 CSS),以便z-index
实际上具有某种含义。 这是一个示例;请注意B
如何出现在A
和C
事件之上,尽管C
具有最大的z-index.
要修改元素的
z-index
,相对于它们所在的容器div
,您必须确保最低的z-indexz-index
大于容器外的最大z-index
。然后,调整其他z-index
来抵消它们。此函数(使用 jQuery)从传递的元素中获取具有最大z-index
的元素:使用:
这应该可以帮助您开始。
编辑:
要填充缺失的
z-index
es,请使用以下代码:它的作用是更改
z-index
为 < code>auto 为集合中最大的z-index
的一加 (elements
);要明白我的意思,查看此演示。
Z-index
only modifies the "layer" (imagine Photoshop) that the element is displayed on. Structurally, the box-model is not changed. Visually, it appears to be, but only if the positioning for the elements have been modified (through CSS) so that thez-index
actually means something. Here's an example; notice howB
appears aboveA
andC
event thoughC
has the greatestz-index
.To modify the
z-index
of elements, relative to the containerdiv
that they are contained in, you have to make sure that the lowestz-index
in the container is greater than the greatestz-index
outside of the container. Then, you adjust the otherz-index
es to offset them. This function (uses jQuery) gets the element with the greatestz-index
, from the passed elements:Use:
That should get you started.
Edit:
To fill in missing
z-index
es, use this code:What it does is it changes elements with a
z-index
ofauto
to one-plus whatever the greatestz-index
is in the collection (elements
);To see what I mean, check out this demo.