Can I use .delay() together with .animate() in jQu

2019-02-04 10:49发布

I have this code, which slides open a basket preview on a website I am working on. It stays open if the user is hovered on it, but I want it to have a two second delay before the callback for my hover is triggered. This is just in case the user didn't want the mouse to leave the basket area.

Below is the code I am using to animate the basket:

$('.cart_button, .cart_module').hover(function(){
    $(".cart_module").stop().animate({top:'39px'},{duration:500});
}, function(){
    $('.cart_module').stop().animate({top: -cartHeight},{duration:500})
});

Here is the code I tried to use, but had no affect:

$('.cart_button, .cart_module').hover(function(){
    $(".cart_module").delay().animate({top:'39px'},{duration:500});
}, function(){
    $('.cart_module').delay().animate({top: -cartHeight},{duration:500})
});

4条回答
我只想做你的唯一
2楼-- · 2019-02-04 11:23

I've always managed this kind of things with the help of core setTimeout and clearTimeout js functions.

Here is an example on jsFiddle

Take a look at jquery.hoverIntent plugin too, it gives you a timeout on hover and out events

查看更多
贼婆χ
3楼-- · 2019-02-04 11:29

Looks like there may have been updates to jQuery in this vein since 2011. In Chrome I can use this sans timeout calls:

$('.thing').hover(function(){
    $(".thing").delay(2000).animate({top:'39px'},{duration:500});
}
查看更多
干净又极端
4楼-- · 2019-02-04 11:38

If you add the stop before the delay it works just fine:

$('.cart_button, .cart_module').hover(function() {
    $('.cart_module').stop(true, true).delay(100).animate({top:'39px'}, 400);
  },
  function() {
    $('.cart_module').stop(true, true).animate({top: -cartHeight}, 250);
});
查看更多
何必那么认真
5楼-- · 2019-02-04 11:39

How long do you want it to delay for????

$('.cart_button, .cart_module').hover(function(){
            $(".cart_module").delay(2000).animate({top:'39px'},{duration:500}); //two seconds
        }, function(){
            $('.cart_module').delay(2000).animate({top: -cartHeight},{duration:500}) //two seconds 
    });
查看更多
登录 后发表回答