如何制作jquery无限动画?

发布于 2024-10-12 16:40:37 字数 392 浏览 3 评论 0原文

我正在尝试实现一个带有无限循环的 jQuery 函数,以 3 种颜色为主体背景设置动画。我想不出一个好的、干净的解决方案。 像这样的东西吗?

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

有什么想法吗?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(10

甜尕妞 2024-10-19 16:40:37
$(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();
});
$(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();
});
旧情勿念 2024-10-19 16:40:37

您可以消除嵌套,但解决方案有点胖:

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)
}

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)
}
噩梦成真你也成魔 2024-10-19 16:40:37
$(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();
});

演示:http://jsfiddle.net/bHEVr/

$(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/

遥远的绿洲 2024-10-19 16:40:37
$(".elementsToAnimate").each(function setAnim(){
    $(this).
            animate({backgroundColor:'#ffcc00'},500).
            animate({backgroundColor:'#eeeeee'},500).
            animate({backgroundColor:'#3b5998'},500,setAnim);
});
$(".elementsToAnimate").each(function setAnim(){
    $(this).
            animate({backgroundColor:'#ffcc00'},500).
            animate({backgroundColor:'#eeeeee'},500).
            animate({backgroundColor:'#3b5998'},500,setAnim);
});
绝情姑娘 2024-10-19 16:40:37

我宁愿使用事件驱动的方法:

$(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');
});

观看此解决方案的实际应用:

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

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/

鲸落 2024-10-19 16:40:37

在 animate() 的回调中调用 animate 函数。

请参阅 jQuery 论坛 中的此示例

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

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

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();
碍人泪离人颜 2024-10-19 16:40:37
function blabla(){
 $('body').animate({backgroundColor:'#ffcc00'}, 500, function(){
        $('body').animate({backgroundColor:'#eeeeee'}, 500, function(){
           $('body').animate({backgroundColor:'#3b5998'}, 0,function (){
               setTimeout(blabla,500);
           });
       });
   });

}

未经测试

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

}

UNTESTED

感受沵的脚步 2024-10-19 16:40:37

试试这个: 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();  

});

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();  

});
电影里的梦 2024-10-19 16:40:37

我知道已经过去了很多年,但我认为这对于某些人来说仍然是一个问题,就像我使用 jquery v1.10.2 时一样。
不管怎样,经过几个小时的尝试处理它,我最终使用了以下代码来实现多层背景 这个插件

// 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);

命名方案如下:我创建嵌套的DIV并在HTML中给它们ID。在 JS 部分,相同的 ID 用于在包含所有动画完成时自调用函数的对象中键入属性。 此处演示。

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.

苍景流年 2024-10-19 16:40:37

我强烈推荐 jQuery 计时插件 (2KB) (GitHub & 文档)。

它提供了易于使用的无限动画等等。看看:

$(document).ready(function(){    

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

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)
});
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文