jquery 为多个元素设置动画

发布于 2024-08-18 00:48:44 字数 691 浏览 12 评论 0原文

我正在尝试执行以下操作:

我在页面上有两组 DOM 元素。第一组中的所有元素都带有 display-element 类,第二组元素带有 edit-element 类。

现在我有一个按钮(id=edit-button)。当我单击此按钮时,我希望所有 display-elements 淡出,并显示所有 edit-elements

我自然而然地这么做了:

$('#edit-button').click(function(){
    $('.display-element').fadeOut(300, function() {
        $('.edit-element').fadeIn(300);
    });
});

我惊讶地发现这并没有按预期工作。事情是这样的:一旦第一个带有 display-element 类的元素淡出,所有 edit-elements 就开始淡入。

现在有没有简单的方法(也许我错过了文档中的某些内容),使用它我可以让所有 edit-elements 仅在所有 display-elements 淡出后才开始淡入? >

谢谢
杰瑞赫

I'm trying to do the following:

I have two sets of DOM elements on a page. All elements in the first set carry the class display-element, and the second set of elements carry the class edit-element.

Now I have a button (id=edit-button). When I click this button, I want all the display-elements to fade out, and all the edit-elements to appear.

I naturally did:

$('#edit-button').click(function(){
    $('.display-element').fadeOut(300, function() {
        $('.edit-element').fadeIn(300);
    });
});

I found to my surprise that this did not work as expected. This is what happened: as soon as the first element with class display-element faded out, all the edit-elements started fading in.

Now is there any easy way (perhaps something in the documentation I missed) using which I can have all the edit-elements start fading in only after all display-elements have faded out?

Thanks
jrh

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

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

发布评论

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

评论(4

窗影残 2024-08-25 00:48:44

好吧,由于没有找到任何“漂亮的 API 方法”,我决定这样做:

$('#edit-button').click(function() {
    var displays = $('.display-element');
    var counter = 0;
    var max = displays.length;
    displays.fadeOut(300, function() {
        counter++;
        if( counter>=max ) $('.input-element').fadeIn(300);
    }
});

这本质上是 @shylent 的答案,还有更多“javascript-fu”:)

Well, having not found any 'nifty API method', I resolved to this:

$('#edit-button').click(function() {
    var displays = $('.display-element');
    var counter = 0;
    var max = displays.length;
    displays.fadeOut(300, function() {
        counter++;
        if( counter>=max ) $('.input-element').fadeIn(300);
    }
});

This is essentially @shylent's answer, with more 'javascript-fu' :)

乱了心跳 2024-08-25 00:48:44

为什么会发生这种事,真的吗?我的意思是,看起来您正在同时启动动画(毕竟只有一次对 .fadeOut 的调用,对吧?)。我想,这是因为 $()​​ 生成一个数组,当您调用 .fadeOut 时,jQuery 会迭代该数组并执行它对其每个元素执行的任何操作。从第一个开始。由于回调会“附加”到每个动画,因此您会看到第一个回调的可见效果。

我想,您可以通过自己迭代返回元素的数组并确保仅将回调附加到最后一个元素来回避这一问题。

$('#edit-button').click(function(){
    var display_elements = $('.display-element');
    var len = display_elements.length;
    for (var i = 0; i < len-1; i++) {
        $(display_elements[i]).fadeOut(300); // no callback
    }
    $(display_elements[len-1]).fadeOut(300, function() {
        $('.edit-element').fadeIn(300);
    }); // last element - attach callback
});

如果代码关闭,我很抱歉,我必须承认,我的 javascript-fu 不是很强:)

Why would this happen, really? I mean, it appears, that you are starting the animations at the same time (there is only one call to .fadeOut after all, right?). I guess, this is because $() yields an array and when you call .fadeOut, jQuery iterates through the array and performs whatever it performs for each of its elements. Starting from the first. Since the callback gets "attached" to every animation, you see the visible effect of the first callback.

I guess, you could sidestep this by iterating through the array of the returned elements yourself and making sure, that you attach the callback only to the last one.

$('#edit-button').click(function(){
    var display_elements = $('.display-element');
    var len = display_elements.length;
    for (var i = 0; i < len-1; i++) {
        $(display_elements[i]).fadeOut(300); // no callback
    }
    $(display_elements[len-1]).fadeOut(300, function() {
        $('.edit-element').fadeIn(300);
    }); // last element - attach callback
});

I am sorry if the code is off, I must admit, that my javascript-fu is not very strong :)

记忆之渊 2024-08-25 00:48:44

解决方案是在淡入函数上使用 setTimeout

$('#edit-button').click(function(){
    $('.display-element').fadeOut(300);
setTimeout("$('.edit-element').fadeIn(300);", 300);
});

该 javascript 函数会将淡入函数的触发延迟 300 毫秒

The solution is to use a setTimeout on the fading in function

$('#edit-button').click(function(){
    $('.display-element').fadeOut(300);
setTimeout("$('.edit-element').fadeIn(300);", 300);
});

That javascript function will delay the trigger of the the fading in function for 300 miliseconds

恬淡成诗 2024-08-25 00:48:44

我刚刚运行了一个类似的问题,发现“step”回调选项产生了所需的行为。

step:在动画的每个步骤之后调用的函数。

^ http://api.jquery.com/animate/

向下滚动到演示以了解步骤用法并您将看到它如何使您能够在单个对象上调用动画,然后使用步骤在动画的每个步骤中更新您的其余集。这样,当动画完成时,您只会触发一个回调。

I just ran a similar issue and found the "step" callback option yielded the desired behavior.

step: A function to be called after each step of the animation.

^ http://api.jquery.com/animate/

Scroll down to the demo for step usage and you'll see how it will enable you to call animate on a single object then use step to update the rest your set at each step of the animation. This way you only have one callback fired when the animation completes.

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