var bubble = [$('#bubble1'), $('#bubble2'), $('#bubble3'), $('#bubble4'), $('#bubble5'), $('#bubble6')];
var bubbleFirst = 0;
var visibleBubbles = 3;
var bubbleHeight = 200;
var bubbleTimeDelay = 5000;
var bubbleTimer = setTimeout(function(){animateBubbles();}, bubbleTimeDelay/2);
function animateBubbles(){
clearTimeout(bubbleTimer);//stop from looping before this is finished
for(var i = 0; i < visibleBubbles + 1; i++){
count = i + bubbleFirst;
if (count >= bubble.length) count = count - bubble.length;//keep index inside array
bubble[count].animate({top:'-=' + bubbleHeight}, 2000);
}
bubble[bubbleFirst].css('top', '600px');//put elements moving off top to bottom
//resetBubbles();
bubbleFirst++;
if(bubbleFirst >= bubble.length) bubbleFirst = 0;//bubbles have looped
bubbleTimer = setTimeout(function(){animateBubbles();}, bubbleTimeDelay);//start looping again
}
- bubble1與
top: 0px;
- bubble2開始開始與
top: 200px;
- bubble3與
top: 400px;
- bubble4-6開始開始於
top: 600px;
- 都是
position: absolute
在包裝div
代碼轉儲道歉。我的問題都是圍繞着16行:無法更改在Jquery的CSS
bubble[bubbleFirst].css('top', '600px');
是看似永遠不會執行這個代碼,還有在我的控制檯沒有錯誤,我已經驗證了泡沫[bubbleFirst]使用的console.log返回正確的元素。 (這是一個div)
我目前使用的一種變通方法,但它不是動態:
function resetBubbles(){
/*bubble[bubbleFirst].css('top', '600px');//put elements moving off top to bottom*/
if(bubble[0].css('top') == '-200px') bubble[0].css('top', '600px');
if(bubble[1].css('top') == '-200px') bubble[1].css('top', '600px');
if(bubble[2].css('top') == '-200px') bubble[2].css('top', '600px');
if(bubble[3].css('top') == '-200px') bubble[3].css('top', '600px');
if(bubble[4].css('top') == '-200px') bubble[4].css('top', '600px');
if(bubble[5].css('top') == '-200px') bubble[5].css('top', '600px');
}
我不知道爲什麼,這是行不通的,它可能是我的一個邏輯上的錯誤。但我無法爲我的生活找到它。任何幫助,將不勝感激。謝謝你的時間。
你有什麼錯誤嗎?哪一行代碼正在執行? – Halcyon 2013-03-04 17:14:50
所有行都正在執行,如果我運行resetBubbles函數,代碼將按照我的要求工作,但當元素數量需要增加時,它無效。第16行是未按需要運行的行。 – gaynorvader 2013-03-04 17:16:39