我在 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.
发布评论
评论(2)
我会看一下 jquery 中的 Promise 函数,以确保所有元素都已完成动画:http://api. jquery.com/promise/
对于您的示例:
工作示例: 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:
Working example: http://jsfiddle.net/cm3pv/6/
我尝试了 Gary.S 接受的答案,但在动画完成之前我的函数仍在触发。我在另一篇文章中找到了建议 如何获取jQuery 等待效果完成?
需要使用
$.when( func ).done( function () { myFunc(); } );
我必须将我的函数放入一个新功能。
我最终使用的看起来像这样(使用你的例子):
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):