2010-08-30 106 views
1

我正在尋找背景顏色的動畫,以便在無限循環中循環varios預定義顏色。jquery,背景顏色轉換循環

我不是一個程序員和新的jQuery的,如果有人可以幫助我弄清楚了這一點,我真的很感激IST

THX!

+0

你可以發佈你已經做了什麼嗎?很難說你需要什麼。 – Iznogood 2010-08-30 00:41:46

回答

2

window.setInterval('functionToChangeColour()',5000);這將每5000毫秒運行一次你的函數,按照你希望改變的方式改變顏色。你可以指定一個對象var obj = setInterval('functionToChangeColour()',5000);你可以指定一個對象var obj = setInterval('functionToChangeColour()',5000);到後來,如果你清除間隔喜歡使用window.clearInterval(OBJ)

2

建立在拜倫的解決方案,並利用該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