1
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
}
  • 气泡1以top: 0px;
  • 气泡2以top: 200px;
  • bubble3 以top: 400px;
  • 泡泡4-6开始top: 600px;
  • position: absolute在一个包装器 div 中

为代码转储道歉。我的问题都集中在第 16 行:

bubble[bubbleFirst].css('top', '600px');

这段代码似乎从未执行过,我的控制台中没有错误,并且我已经验证了 bubble[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');
}

我不知道为什么这不起作用,这可能是我的逻辑错误。但我一辈子都找不到它。任何帮助,将不胜感激。谢谢你的时间。

4

2 回答 2

1

对动画的调用是否可能与您自定义设置 top 属性冲突?IE。您设置了它,但top立即再次被animate. 您可以将回调传递给animate动画完成后将运行的回调。那是你应该重置你的气泡位置的时候。

function animate_bubble(index) {
    bubble[index].animate({ top: "0px" }, 2000 /* 2 seconds */, function () {
        bubble[index].css({ top: "600px" });
        animate_bubble(index);
    }
}
animate_bubble(0);
animate_bubble(1);
// etc .. probably do this in a for-loop

您需要找到一些方法来取消动画,但应该不会太难。

于 2013-03-04T17:19:12.257 回答
0

问题是bubbleFirst在动画完成之前增加了。我将我的功能更改为以下内容:

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
        if (count == bubbleFirst) 
        {
            bubble[count].animate({top:'-=' + bubbleHeight, opacity: 0}, bubbleAnimationSpeed, function(){
                bubble[bubbleFirst].css('top', displayHeight+'px');
                bubble[bubbleFirst].css('opacity', '1'); 
            });
        }
        else if(i == visibleBubbles)
        {
            bubble[count].animate({top:'-=' + bubbleHeight}, bubbleAnimationSpeed, function(){
                bubbleFirst++;
                if(bubbleFirst >= bubble.length) bubbleFirst = 0;//bubbles have looped
                bubbleTimer = setTimeout(function(){animateBubbles();}, bubbleTimeDelay);/*start looping again*/
            });
        }
        else bubble[count].animate({top:'-=' + bubbleHeight}, bubbleAnimationSpeed);
    }       
}

谢谢你们的意见!

于 2013-03-04T17:54:33.533 回答