在删除发生之前,jQuery slideUp() . move()似乎不显示 SlideUp 动画

我有这行 JavaScript,我看到的行为是,selectedLi立即消失,没有“滑动”。这不是我期待的行为。

我应该做什么,以便 selectedLi幻灯片之前,它被删除?

selectedLi.slideUp("normal").remove();
56786 次浏览

Might be able to fix it by putting the call to remove in a callback arg to slideUp?

e.g

selectedLi.slideUp("normal", function() { $(this).remove(); } );
selectedLi.slideUp(200, this.remove);

You need to be more explicit: rather than saying "this" (which I agree should work), you should do this:

$("#yourdiv").slideUp(1000, function() {
$(this).remove();
});

The simplest way is calling the "remove()" function inside slideUp as a parameter like others have said, as this example:

$("#yourdiv").slideUp("normal", function() {
$(this).remove();
});

It is a must to call it inside the anonymous function() to prevent remove() to be executed before the slideUp has ended. Another equal way is to use the jQuery function "promise()". Better for those who like self-explanatory code, like me ;)

$("#yourdiv").slideUp("normal").promise().done(function() {
$(this).remove();
});

Using promises you can also wait for multiple animations to get finished, e.g.:

selectedLi.slideUp({duration: 5000, queue: false})
.fadeOut({duration: 3000, queue: false})
.promise().done(function() {
selectedLi.remove()
})