开发者

Using a .delay() function before .css() manipulation in jQuery

开发者 https://www.devze.com 2023-01-01 03:24 出处:网络
I have this: var $ul = $(this).children(\"ul\"); $ul.animate({opacity: 0}, 1000); $ul.delay(1000).css(\"display\", \"none\")

I have this:

var $ul = $(this).children("ul");
$ul.animate({opacity: 0}, 1000);
$ul.delay(1000).css("display", "none")

It's for my dropdown menu, and I want it to fade off before it disappears using the display: none; CSS property. However, it appears 开发者_运维百科that the .delay() cannot be used on the .css(); it doesn't work, the $ul just disappears right away.

I can't use $ul.animate({opacity: 0, display: "none"}, 1000); either.


The last parameter to animate is a callback that's called when the animation is complete, so you can use it like so:

$ul.animate({opacity: 0}, 1000, function() { 
    $(this).css("display", "none")
});


Like codeka said the callback is the correct approach here, but there are some shortcut methods built in you can use to simplify it further, .fadeOut() and .hide(), like this:

$ul.fadeOut(1000, function() {
  $(this).hide();
});
0

精彩评论

暂无评论...
验证码 换一张
取 消