【发布时间】:2010-08-30 00:30:45
【问题描述】:
我希望为背景颜色设置动画,以便它在无限循环中循环通过各种预定义颜色。
我不是程序员,也不是 jquery 的新手,如果有人能帮我解决这个问题,我会非常感激 ist
谢谢!
【问题讨论】:
-
您能发布您已经完成的工作吗?很难准确地说出您需要什么。
标签: jquery gradient transition
我希望为背景颜色设置动画,以便它在无限循环中循环通过各种预定义颜色。
我不是程序员,也不是 jquery 的新手,如果有人能帮我解决这个问题,我会非常感激 ist
谢谢!
【问题讨论】:
标签: jquery gradient transition
以 Byron 的解决方案为基础并利用 color animations plugin:
// The array of colours and current index within the colour array
// This array can be hex values or named colours
var colours = ['red', 'orange', 'yellow', 'green', 'blue', 'purple'];
var currIndex = 0;
// The element to animage the background-color of
var elem = $('#element-id');
// Infinitely animate the background-color of your element every second
var loop = setInterval(function(){
elem.animate({backgroundColor: colours[currIndex++]});
// Set the current index in the colour array, making sure to loop
// back to beginning once last colour is reached
currIndex = currIndex == colours.length ? 0 : currIndex;
}, 1000);
你可以看到in action here。
【讨论】:
window.setInterval('functionToChangeColour()', 5000);这将每 5000 毫秒运行一次函数,按照您希望的方式更改颜色。
您可以分配一个对象 var obj = setInterval('functionToChangeColour()', 5000);如果你喜欢使用 window.clearInterval(obj) 来清除间隔
【讨论】: