回调函数触发得太早

发布于 2024-12-09 22:29:37 字数 957 浏览 2 评论 0 原文

我在 jQuery 中有这个简单的函数:

function detailspage(page) {

  if (page != checkcurrent) {

    checkcurrent = page;

    $('div#details').children("div").slideUp("slow", function() {

        $('div#details').children("div"+page).slideDown("slow");

    });

  };

};

我将三个

放入另一个名为
中>,默认情况下它们都是向上滑动并看不见的。我现在希望在单击每个按钮时使它们向下滑动。当然,打开的
必须首先向上滑动并移出视线,然后再向下滑动新的
。这就是为什么我尝试了上面这个简单的回调函数。

page 变量包含其中一个 div 的 ID,例如 #info#cast_crew。 )

但它不起作用: 您可以通过单击页面底部左侧的三个按钮来查看错误,名为“演员和工作人员”、“信息”和“画廊”

除了似乎不会造成任何延迟的回调函数之外,这一切都可以正常工作。回调没有任何效果。我只是希望在当前框的向上滑动完成时开始新框的向下滑动。

怎么了?为什么我的回调不起作用?

谢谢。

I have this simple function in jQuery:

function detailspage(page) {

  if (page != checkcurrent) {

    checkcurrent = page;

    $('div#details').children("div").slideUp("slow", function() {

        $('div#details').children("div"+page).slideDown("slow");

    });

  };

};

I have put three <div>'s inside another <div> called <div id="details">, and they are all slided up and out of sight by default. I now wish to make each of them slide down, when a button for each of them is clicked. Of course a <div> that is open must first be slided up and out of sight, before the new one is slided down. That is why I have tried this simple callback function above.

(The page variable contains an id for one of the div's, like #info or #cast_crew.)

But it doesn't work: You can see the error by clicking the three buttons on the left in the bottom of the page, named "Cast & crew", "Info" and "Galleri".

It all works apart from the callback function that doesn't seem to cause any delay. The callback has no effect. I simply want the slidedown of the new box to start when the slideup of the current box is finished.

What is wrong? Why doesn't my callback work?

Thank you.

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

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

发布评论

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

评论(2

染年凉城似染瑾 2024-12-16 22:29:37

我会看一下 jquery 中的 Promise 函数,以确保所有元素都已完成动画:http://api. jquery.com/promise/

对于您的示例:

function detailspage(page) {
    if (page != checkcurrent) {
        // so we only have to search the dom once
        var details = $('div#details');
        checkcurrent = page;
        details.children("div").slideUp("slow").promise().done(
            function() {
                $('div' + page).slideDown("slow");
            });
    };
};

工作示例: http://jsfiddle.net/cm3pv /6/

I would take a look at the promise function in jquery to ensure all elements have completed the animation: http://api.jquery.com/promise/

For your example:

function detailspage(page) {
    if (page != checkcurrent) {
        // so we only have to search the dom once
        var details = $('div#details');
        checkcurrent = page;
        details.children("div").slideUp("slow").promise().done(
            function() {
                $('div' + page).slideDown("slow");
            });
    };
};

Working example: http://jsfiddle.net/cm3pv/6/

无人问我粥可暖 2024-12-16 22:29:37

我尝试了 Gary.S 接受的答案,但在动画完成之前我的函数仍在触发。我在另一篇文章中找到了建议 如何获取jQuery 等待效果完成?

需要使用 $.when( func ).done( function () { myFunc(); } );

我必须将我的函数放入一个新功能。

我最终使用的看起来像这样(使用你的例子):

$.when( $('div#details').children("div").slideUp("slow") ).done(){ function() {
    $('div#details').children("div"+page).slideDown("slow");
});

I tried Gary.S's accepted answer, but my function was still firing before the animation was complete. I found a suggestion on another post How to get jQuery to wait until an effect is finished?

Needed to use $.when( func ).done( function () { myFunc(); } );

I HAD to put my function in a new function.

What I ended up using looked like this (to use your example):

$.when( $('div#details').children("div").slideUp("slow") ).done(){ function() {
    $('div#details').children("div"+page).slideDown("slow");
});
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文