绝对定位元素上同时淡入/淡出的 JQuery 错误

发布于 2024-11-07 06:49:22 字数 427 浏览 0 评论 0原文

我有一组元素想要一个接一个地显示,淡出第一个元素,同时淡入第二个元素,并在到达最后一个元素时循环它们

如果我绝对定位第一个元素,同时淡出,第二个元素不会定位没有任何问题,除非是最后一个。有一个错误(?)使最后一个项目(应该淡出)在将位置设置为绝对位置后使用空间,就好像它具有相对位置一样。

您可以在此处查看该错误: http://jsfiddle.net/XekVc/

Chrome 12.0 中存在此行为(我认为是最后一个版本)和 FF 3.6.17

我不想让它们全部绝对定位,因为我的应用程序中的容器有其他相对定位的元素,我必须在此之后调整,所以这是我的最后一个选择。

有谁知道这是我的代码中的错误,还是 jquery/浏览器的渲染引擎问题?

I have a collection of elements I want to show one after another, fading out the first while fading in the second simultaneously, and cycle them when reaching the last one

If I position absolutely the first element, while fading out, the second one doesn't have any problems positioning, EXCEPT if it is the last one. There is a bug (?) that makes the last item, that should be fading out, to use the space as if it has a relative position, after setting the position to absolute.

You can see the bug here: http://jsfiddle.net/XekVc/

This behavior is present in Chrome 12.0 (I think is last version) and FF 3.6.17

I don't want to make all of them absolutely positioned, since the container in my application has other relatively positioned elements which I would have to adjust after this, so it is my last alternative.

Does anyone have an idea if this is a bug on my code, or is this a jquery / browser's render engine issue?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(3

无敌元气妹 2024-11-14 06:49:22

在玩小提琴时,我可以通过在更改其位置之前执行 element.prependTo(element.parent()) 将离开的元素放置在即将到来的元素之前来避免该错误。

http://jsfiddle.net/e253r/

这可能是一种解决方法,但我仍然真正感兴趣找出它是否是一个错误以及在哪里......

Playing with the fiddle, I can avoid the bug by placing the leaving element before the coming one, by doing a element.prependTo(element.parent()) before changing its positioning.

http://jsfiddle.net/e253r/

This can be a workaround, but I'm still genuinely interested in finding if it is a bug and where...

坐在坟头思考人生 2024-11-14 06:49:22

编辑所以我决定查看.animate()路线:

.thing {
    background-color: #9E9E9E;
    border: 2px solid #393939;
    padding: 30px;
    width: 140px;
    height: 70px;
    position: absolute;
}

$(function() {
    var index = 0;
    $('.thing').css("opacity", "0");
    var current = $('.thing:first-child').show();
    $('.thing:last-child').css({
        "position": "static"
    });
    setInterval(function() {
        index = (index + 1) % 3;
        var next = $($('.thing')[index]);
        current.animate({
            opacity: 0
        }, 1500);
        next.animate({
            opacity: 1
        }, 1500);

        current = next;
    }, 2000);
})

http://jsfiddle.net/XekVc/16/

似乎有效

EDIT so I decided to check out the .animate() route:

.thing {
    background-color: #9E9E9E;
    border: 2px solid #393939;
    padding: 30px;
    width: 140px;
    height: 70px;
    position: absolute;
}

$(function() {
    var index = 0;
    $('.thing').css("opacity", "0");
    var current = $('.thing:first-child').show();
    $('.thing:last-child').css({
        "position": "static"
    });
    setInterval(function() {
        index = (index + 1) % 3;
        var next = $($('.thing')[index]);
        current.animate({
            opacity: 0
        }, 1500);
        next.animate({
            opacity: 1
        }, 1500);

        current = next;
    }, 2000);
})

http://jsfiddle.net/XekVc/16/

seems to work

心奴独伤 2024-11-14 06:49:22

不知道为什么会发生这种情况。除了绝对定位还能设置top:0吗?容器位置也必须设置

http://jsfiddle.net/XekVc/17/

Not sure why this happens. Can you set top: 0 in addition to absolute positioning? The container position would have to be set too

http://jsfiddle.net/XekVc/17/

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