jQuery .add 方法

发布于 2024-10-09 02:43:09 字数 1017 浏览 0 评论 0原文

我正在编写一个简单的 jQuery 插件,它在屏幕中心显示具有特定类的图像(单击时为全尺寸),并在图像周围显示灰色覆盖层。当我们点击覆盖层时,它就会消失。现在我使用这段代码:

$("#overlay").click(function(){
        $(this).animate({
            opacity: "0"
        }, 500, function(){
            $(this).css({
                display: "none"
            });
        });

        $("#imgcontainer").animate({
            opacity: "0"
        }, 500, function(){
            $(this).css({
                display: "none"
            });

        });

    });

imgcontainer 保存我们的图像。我试图使用这个更简单的代码,但它不起作用:

$("#overlay").click(function(){
        $(this).add("#imgcontainer").animate({
            opacity: "0"
        }, 500, function(){
            $(this).css({
                display: "none"
            });
        });
    });

为什么它不起作用?

更新:
正确代码:

$("#overlay").click(function(){
        $("#imgcontainer").add(this).fadeOut();
    });

非常感谢您的回复。这种行为(bug?)确实非常令人惊讶。

I'm writing a simple jQuery plugin that displays images with a specific class in the center of the screen (when clicked, full-sized) with a gray overlay around the image. When we click on the overlay it disappears. Now I use this piece of code:

$("#overlay").click(function(){
        $(this).animate({
            opacity: "0"
        }, 500, function(){
            $(this).css({
                display: "none"
            });
        });

        $("#imgcontainer").animate({
            opacity: "0"
        }, 500, function(){
            $(this).css({
                display: "none"
            });

        });

    });

imgcontainer holds our image. I was trying to use this easier code, but it doesn't work:

$("#overlay").click(function(){
        $(this).add("#imgcontainer").animate({
            opacity: "0"
        }, 500, function(){
            $(this).css({
                display: "none"
            });
        });
    });

Why it doesn't work?

UPDATE:
Correct code:

$("#overlay").click(function(){
        $("#imgcontainer").add(this).fadeOut();
    });

Thanks a lot for replies. This behavior (bug?) is very surprising indeed.

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

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

发布评论

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

评论(1

剪不断理还乱 2024-10-16 02:43:09

如果您使用的是 v1.4.x,由于 问题 #7853 我刚刚在他们的今天早些时候的错误跟踪器(什么时间!)。您可以通过更改以下内容来修复它

$(this).add("#imgcontainer").animate(...


$(this).add($("#imgcontainer")).animate(...

更新:这是一个更好(更有效)的方法,正如 Nick 在下面指出的:(

$("#imgcontainer").add(this).animate(...

另请参阅他关于 fadeOut.)

在 jQuery 1.3.2 及更早版本中,它会按您的预期工作。我不知道 1.4.x 的事情是一个错误还是故意的不兼容更改。

问题:为什么不淡出容器呢?如果它包含图像,那么图像也会褪色,不是吗?就像这个例子一样? (我不做很多动画,所以如果我错过了一些微妙的东西,我很抱歉。) 没关系,你从来没有说过#overlay是图像,这对我来说是一个无效的假设。

If you're using v1.4.x, because of issue #7853 I just raised in their bug tracker earlier today (what timing!). You can fix it by changing this:

$(this).add("#imgcontainer").animate(...

to

$(this).add($("#imgcontainer")).animate(...

Update: Here's a better (more efficient) way, as Nick pointed out below:

$("#imgcontainer").add(this).animate(...

(See also his note about fadeOut.)

In jQuery 1.3.2 and earlier, it would have worked as you expected. I don't know whether the 1.4.x thing is a bug or an intentional incompatible change.

Question though: Why not just fade out the container? If it contains the image, that should fade the image too, shouldn't it? As in this example? (I don't do a lot of animation, so apologies if I've missed something subtle.) Never mind, you never said #overlay was the image, that was an invalid assumption on my part.

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