使用 CSS/Javascript 创建堆栈视图控件

发布于 2024-09-19 00:38:18 字数 247 浏览 10 评论 0原文

我想创建一个堆栈类型控件,其中一系列堆叠的子 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

青巷忧颜 2024-09-26 00:38:24

如果您只希望一个 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.

落在眉间の轻吻 2024-09-26 00:38:23

给定 div 中的元素如何具有
比某物更高的 z 顺序
放置在该 div 之后?它只是
忽略嵌套的 div 层次结构?

Z-index 仅修改显示元素的“图层”(想象一下 Photoshop)。从结构上来说,盒子模型没有改变。从视觉上看,似乎是这样,但前提是元素的位置已被修改(通过 CSS),以便 z-index 实际上具有某种含义。 这是一个示例;请注意 B 如何出现在 AC 事件之上,尽管 C 具有最大的 z-index.

要修改元素的 z-index,相对于它们所在的容器 div,您必须确保最低的 z-indexz-index 大于容器外的最大 z-index。然后,调整其他 z-index 来抵消它们。此函数(使用 jQuery)从传递的元素中获取具有最大 z-index 的元素:

function getTopElement(elems){
    // Store the greates z-index that has been seen so far
    var maxZ = 0;
    // Stores a reference to the element that has the greatest z-index so far
    var maxElem;
    elems.each(function(){
        var z = parseInt($(this).css("z-index"), 10);
        // Ignore z-index of auto
        if (!isNaN(z)){
            if (parseInt($(this).css("z-index"), 10) > maxZ) {
                maxElem = $(this);
                maxZ = parseInt($(this).css("z-index"), 10);
                alert (maxZ);
            }
        }
    });
    return maxElem;
};

使用:

var topDiv = getTopElement($("div"));
var topZIndex = topDiv.css("z-index");

这应该可以帮助您开始。

编辑:

要填充缺失的 z-indexes,请使用以下代码:

var elements = $("div");
var newZ = parseInt(getTopElement(elements).css("z-index"), 10);

elements.each(function(){
    if (isNaN(parseInt($(this).css("z-index"),10))){
        $(this).css("z-index", ++newZ);
    }

});

它的作用是更改 z-index 为 < code>auto 为集合中最大的 z-index 的一加 (elements);

要明白我的意思,查看此演示

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?

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 the z-index actually means something. Here's an example; notice how B appears above A and C event though C has the greatest z-index.

To modify the z-index of elements, relative to the container div that they are contained in, you have to make sure that the lowest z-index in the container is greater than the greatest z-index outside of the container. Then, you adjust the other z-indexes to offset them. This function (uses jQuery) gets the element with the greatest z-index, from the passed elements:

function getTopElement(elems){
    // Store the greates z-index that has been seen so far
    var maxZ = 0;
    // Stores a reference to the element that has the greatest z-index so far
    var maxElem;
    elems.each(function(){
        var z = parseInt($(this).css("z-index"), 10);
        // Ignore z-index of auto
        if (!isNaN(z)){
            if (parseInt($(this).css("z-index"), 10) > maxZ) {
                maxElem = $(this);
                maxZ = parseInt($(this).css("z-index"), 10);
                alert (maxZ);
            }
        }
    });
    return maxElem;
};

Use:

var topDiv = getTopElement($("div"));
var topZIndex = topDiv.css("z-index");

That should get you started.

Edit:

To fill in missing z-indexes, use this code:

var elements = $("div");
var newZ = parseInt(getTopElement(elements).css("z-index"), 10);

elements.each(function(){
    if (isNaN(parseInt($(this).css("z-index"),10))){
        $(this).css("z-index", ++newZ);
    }

});

What it does is it changes elements with a z-index of auto to one-plus whatever the greatest z-index is in the collection (elements);

To see what I mean, check out this demo.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文