How to make a jquery infinite animation?

送分小仙女□ 提交于 2019-11-26 05:58:36

问题


I\'m trying to implement a jQuery function with an infinite loop to animate the body background with 3 colours. I cannot think of a nice and clean solution. Something like this?

$(document).ready(function(){                
     $(\'body\').animate({backgroundColor:\'#ffcc00\'}, 500, function(){
        $(\'body\').animate({backgroundColor:\'#eeeeee\'}, 500, function(){
           $(\'body\').animate({backgroundColor:\'#3b5998\'}, 500);
       });
   });
});

Any idea?


回答1:


You can eliminate the nesting, but the solution is a little fatter:

var cols = "#ffcc00,#eeeeee,#3b5998".split(",")
var cPos = 0

$(document).ready(function() {
   swapC()
}    

function swapC() {
    $('body').animate({ backgroundColor:cols[cPos] }, 500)
    cPos++
    if (cPos == cols.length) {
        cPos = 0
    }
    window.setTimeout(function() { swapC() }, 500)
}



回答2:


$(document).ready(function(){
    function animate() {
        $('body').animate({backgroundColor:'#ffcc00'}, 500, function(){
            $('body').animate({backgroundColor:'#eeeeee'}, 500, function(){
                $('body').animate({backgroundColor:'#3b5998'}, 500, function(){
                    animate();
                });
            });
        });
    }
    animate();
});



回答3:


$(document).ready(function(){
    colors = ['#FFB30C', '#58EC00', '#0087EC', '#EEEEEE', '#FF5A00' ]
    var i = 0;
    animate_loop = function() {
            $('body').animate({backgroundColor:colors[(i++)%colors.length]
            }, 500, function(){
                        animate_loop();
            });
    }
    animate_loop();
});

Demo: http://jsfiddle.net/bHEVr/




回答4:


$(".elementsToAnimate").each(function setAnim(){
    $(this).
            animate({backgroundColor:'#ffcc00'},500).
            animate({backgroundColor:'#eeeeee'},500).
            animate({backgroundColor:'#3b5998'},500,setAnim);
});



回答5:


I would rather use an event-driven approach:

$(document).ready(function(){
  $('body').on('color1', function () {
    $(this).animate({backgroundColor:'#ffcc00'}, 500, function(){
      $(this).trigger('color2');
    });
  });

  $('body').on('color2', function () {
    $(this).animate({backgroundColor:'#eeeeee'}, 500, function(){
      $(this).trigger('color3');
    });
  });

  $('body').on('color3', function () {
    $(this).animate({backgroundColor:'#3b5998'}, 500, function(){
      $(this).trigger('color1');
    });
  });

  // Kick-off the infinite loop by firing one of the events
  $('body').trigger('color2');
});

Watch this solution in action:

http://jsfiddle.net/perituswebdesign/f5qeo6db/1/




回答6:


Call the animate functions in the callback of animate().

See this example in the jQuery forum

jQuery.fn.fadeInOut = function() {
        var newOpacity = this.is(":visible") ?  0 : 1;
        this.animate({ opacity: newOpacity }, function() {
                $(this).fadeInOut();
        });
        return this;
};

$("#mydiv").fadeInOut();



回答7:


function blabla(){
 $('body').animate({backgroundColor:'#ffcc00'}, 500, function(){
        $('body').animate({backgroundColor:'#eeeeee'}, 500, function(){
           $('body').animate({backgroundColor:'#3b5998'}, 0,function (){
               setTimeout(blabla,500);
           });
       });
   });

}

UNTESTED




回答8:


I highly recommend the jQuery timing plugin (2KB) (GitHub & Docs).

It provides easy-to-use infinite animations and much more. Have a look:

$(document).ready(function(){    

 $('body').animate({backgroundColor:'#ffcc00'}).wait(500)
          .animate({backgroundColor:'#eeeeee'}).wait(500)
          .animate({backgroundColor:'#3b5998'}).wait(500)
});



回答9:


Try this : http://jsfiddle.net/hBBbr/

$(document).ready(function(){

animate_loop = function animate_loop(){
        $( "#animated_banner" ).animate({
            opacity: 0.1, 

          }, 1000,function(){
               $( "#animated_banner").animate({ opacity: 1},1000)
                 animate_loop();
        } );    
}

animate_loop();  

});



回答10:


I know it's years later but I think this could still be a problem for someone just like it was for me with jquery v1.10.2. Anyway, after a few hours of trying to deal with it, I ended up using the following code for multiple layered backgrounds with this plugin:

// Self-calling functions for animation auto-repeat
var cssanimfx={
    bgb:function(o){
      o=$(o?o:this);
      o.css({backgroundPosition:'0px 0px'}).animate({backgroundPosition:"3000px -3000px"},500000,'linear',cssanimfx[o.attr('id')]);
    },
    bgm:function(o){o=$(o?o:this);o.css({backgroundPosition:'0px 0px'}).animate({backgroundPosition:"3000px -3000px"},250000,'linear',cssanimfx[o.attr('id')])},
    bgf:function(o){o=$(o?o:this);o.css({backgroundPosition:'0px 0px'}).animate({backgroundPosition:"3000px -3000px"},50000,'linear',cssanimfx[o.attr('id')])}
    // ...
}
// Initialize animation
for(id in cssanimfx)cssanimfx[id]('#'+id);

The naming scheme is as follows: I create nested DIVs and give them IDs in the HTML. In the JS part, the same IDs are used for keying the properties in the object containing all the self-calling-on-animation-finish functions. Demo here.



来源:https://stackoverflow.com/questions/4713477/how-to-make-a-jquery-infinite-animation

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!