开发者

jQuery Delaying Queue

开发者 https://www.devze.com 2023-01-07 18:36 出处:网络
I want to delay things like the css from happening but this method wont work. Any ideas o开发者_开发技巧n how I can delay tasks.

I want to delay things like the css from happening but this method wont work. Any ideas o开发者_开发技巧n how I can delay tasks.

$(function() {
    $('.btn').click(function() {
    $('.divOne').animate({"left": "+=400px"}, "fast").delay(800).css("background-color","yellow");
    });
});      


You can use .queue() to stick it on the default animation (fx) queue, like this:

$('.btn').click(function() {
  $('.divOne').animate({"left":"+=400px"}, "fast").delay(800).queue(function(n) {
    $(this).css("background-color","yellow");
    n(); //call next function in the queue, needed if you animate later!
  });
});

You can test it here, all this does is stick it in the fx queue using .queue(function(n)), the n is the next function in the queue, so we're calling it, for example if you didn't do this and added any animation after it, it just wouldn't run, because the next function or .dequeue() isn't called.


Maybe use a callback function on the animate. Once the animation is complete use a setTimeout()

$(function() {
    $('.btn').click(function() {
    $('.divOne').animate({"left": "+=400px"},"fast", function() {
        var $elem = $(this);
        setTimeout(function() {
            $elem.css('background-color','yellow');
        }, 800);
    })
});

This might not be syntactically perfect.

0

精彩评论

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